Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one detect airplane mode on Android?

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?

like image 675
Sean W. Avatar asked Nov 30 '10 22:11

Sean W.


People also ask

Can you tell if a phone is on 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.

How do I know if my Android phone is in airplane mode?

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.

What happens when someone calls you on airplane mode?

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.


2 Answers

/** * 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;  } 
like image 81
Alex Volovoy Avatar answered Sep 25 '22 03:09

Alex Volovoy


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;     }        } 
like image 44
Tiago Avatar answered Sep 25 '22 03:09

Tiago