简体   繁体   English

android-如果手机处于飞行模式或不在网络中,如何获取电话?

[英]android - How do I get the phone if it is in airplane mode or not in the network?

I want to get the signal level. 我想获得信号电平。 I use the following method for this. 为此,我使用以下方法。 But I do not know how to reach the phone if it is in plane mode or out of service. 但是,如果手机处于飞行模式或服务中断,我不知道如何接通手机。 Can you help me? 你能帮助我吗?

public void signalLevel() {
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    List<CellInfo> all = tm.getAllCellInfo();
    String a = all.get(0).getClass().getName();


    if (a.equals("android.telephony.CellInfoLte")) {
        CellInfoLte cellInfoLte = (CellInfoLte) all.get(0);
        CellSignalStrengthLte cellSignalStrengthLte = cellInfoLte.getCellSignalStrength();
        signal = String.valueOf(cellSignalStrengthLte.getDbm() + " dB");
    } else if (a.equals("android.telephony.CellInfoWcdma")) {
        CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) all.get(0);
        CellSignalStrengthWcdma cellSignalStrengthWcdma = cellInfoWcdma.getCellSignalStrength();
        signal = String.valueOf(cellSignalStrengthWcdma.getDbm() + " dB");

    } else if (a.equals("android.telephony.CellInfoGsm")) {
        CellInfoGsm cellInfoGsm = (CellInfoGsm) all.get(0);
        CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength();
        signal = String.valueOf(cellSignalStrengthGsm.getDbm() + " dB");
    }
}

Thanks for your help. 谢谢你的帮助。

Try this: 尝试这个:

private static boolean isAirplaneModeOn(Context context) {
    return Settings.System.getInt(context.getContentResolver(),
       Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}

true if enabled. 如果启用,则为true。

For network check try this : 对于网络检查,请尝试以下操作:

ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();

For Checking for specific network type like Wifi use this : 对于检查特定网络类型(例如Wifi),请使用以下命令:

boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;

Please refer https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html for more details. 请参阅https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html了解更多详细信息。

For Airplane mode : 对于飞行模式:

Approach 1 : Check at runtime 方法1:在运行时检查

public static boolean IsAirplaneModeOn(Context activityContext) {        
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return Settings.System.getInt(context.getContentResolver(), 
                Settings.System.AIRPLANE_MODE_ON, 0) != 0;          
    } else {
        return Settings.Global.getInt(context.getContentResolver(), 
                Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
    }       
}

Approach 2: Register for broadcast receiver : 方法2:注册广播接收者:

IntentFilter airplaneModeIntentFilter = new IntentFilter("android.intent.action.AIRPLANE_MODE");

BroadcastReceiver AirplaneModeReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
            boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
            // isAirplaneModeOn == true, means Airplane mode is turned on.
            // Else AirplaneMode is turned off.
      }
};

activitycontext.registerReceiver(receiver, airplaneModeIntentFilter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM