2

I have seen multiple answers in other questions with this solution to know if airplane mode changed:

IntentFilter intentFilter = new      
IntentFilter("android.intent.action.AIRPLANE_MODE_CHANGED");

BroadcastReceiver receiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
            Log.d("AirplaneMode", "Service state changed");
      }
};

context.registerReceiver(receiver, intentFilter);

But now I want to know if the Air Plane Mode is turned ON or OFF on onReceive method.

Thank you.

GPortas
  • 81
  • 1
  • 6

3 Answers3

4

As stated in this answer, you can check airplane mode state by:

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isAirplaneModeOn(Context context) {        
    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;
    }       
}

if the returned result is true, so airplane mode is ON, and vice versa, so your complete answer would be:

IntentFilter intentFilter = new      
IntentFilter("android.intent.action.AIRPLANE_MODE_CHANGED");

BroadcastReceiver receiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
            Log.d("AirplaneMode", "Service state changed");
            boolean airplane_active = isAirplaneModeOn(context);
      }
};

context.registerReceiver(receiver, intentFilter);
Community
  • 1
  • 1
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
1

Try this.

IntentFilter intentFilter = new      
IntentFilter("android.intent.action.AIRPLANE_MODE_CHANGED");

BroadcastReceiver receiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
            Log.d("AirplaneMode", "Service state changed");
            boolean isAirplaneMode = isAirplaneModeOn(context);
      }
};

context.registerReceiver(receiver, intentFilter);

private static boolean isAirplaneModeOn(Context context) {

   return Settings.System.getInt(context.getContentResolver(),
           Settings.System.AIRPLANE_MODE_ON, 0) != 0;

}

Note

In Jelly Bean 4.2, this setting has moved to Settings.Global

Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
  • Prefer using the constant in the Intent class. Which as of Android 10 is `public static final String ACTION_AIRPLANE_MODE_CHANGED = "android.intent.action.AIRPLANE_MODE";` – cerisier Dec 06 '20 at 12:27
0

by using ContentObserver also we can get

private class SetupContentObserver extends ContentObserver {

private final Uri mDeviceProvisioned = Settings.Global.getUriFor(
        Settings.Global.AIRPLANE_MODE_ON);

public SetupContentObserver(Handler handler) {
    super(handler);
}

void register() {
    getContentResolver().registerContentObserver(mDeviceProvisioned, false, this, UserHandle.USER_ALL);
}

@Override
public void onChange(boolean selfChange, Uri uri, int userId) {
    // do actula logic
}

}

ajay
  • 477
  • 6
  • 14