2

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.

2 Answers2

0

Try this:

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

true if enabled.

huk
  • 220
  • 3
  • 11
0

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 :

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

Please refer https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html for more details.

For Airplane mode :

Approach 1 : Check at runtime

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 :

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);