I have code in my application that detects if Wi-Fi is actively connected. That code triggers a RuntimeException if airplane mode is enabled. I would like to display a separate error message when in this mode anyway. How can I reliably detect if an Android device is in airplane mode?
On Android phones, airplane mode is activated by swiping down from the top of the screen two times to open the Settings panel. There, you'll see an airplane icon. When you activate it, your phone is on airplane mode. On an iPhone, the icon for airplane mode is in the Control Center.
Tap Settings > Network & internet > Airplane Mode to toggle it on or off. Swipe down from your home screen and tap Airplane Mode to switch it on or off. Airplane Mode disables all connections, but you can enable Wi-Fi while keeping cellular data disabled.
When you enable airplane mode you disable your phone's ability to connect to cellular or WiFi networks or to Bluetooth. This means you can't make or receive calls, send texts, or browse the internet.
/** * Gets the state of Airplane Mode. * * @param context * @return true if enabled. */ private static boolean isAirplaneModeOn(Context context) { return Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0; }
By extending Alex's answer to include SDK version checking we have:
/** * Gets the state of Airplane Mode. * * @param context * @return true if enabled. */ @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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With