Android: How to activate the airplane or flight mode

chuong_blog_android_airplane_mode_qr_code

A useful feature of Android seldom used by mobile device owners is the ability to switch on the airplane or flight mode. When activated, this function will prevent any Wi-Fi, mobile network, Bluetooth, or other radio connections from being active—like Near-field communication (NFC), for instance. To determine what type of radio connections will be deactivated by the airplane or flight mode, run “adb shell settings get global airplane_mode_radios” via the Android Debug Bridge (ADB) against any Android 4.1.2 plus mobile device.

There are many usage of the airplane or flight mode—one of which is when Android mobile device owners switch it on right before bedtime to ensure that strong radio waves emitting from their mobile devices are not “cooking” their brain whilst they slept. (For health reasons, consumers should avoid buying any type of mobile device if it does not provide an option to toggle the airplane or flight mode.) This function is also useful during meeting when device owners do not wish to be disturbed or startled by sudden, annoying advertisements, or notifications propagated automatically through an active Wi-Fi or mobile network connection on the mobile device. It’s also the first thing travellers would do once they boarded an airplane. It’s no wonder that this wonderful function has been incorporated into practically all mobile operating systems—Android included.

For Android, this function was easily toggled from Android 2.1 (Eclair) to Android 4.1 (Jellybean). As such, Android developers created a plethora of applications just to cater for this functionality. To toggle airplane or flight mode:

@SuppressWarnings("deprecation")
public static void setFlightMode(Context context) {
   boolean enabled = isFlightModeEnabled(context);
   Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, enabled ? 0 : 1);
   Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
   intent.putExtra("state", !enabled);
   context.sendBroadcast(intent);
}

To check if the airplane or flight mode has been activated:

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private static boolean isFlightModeEnabled(Context context) {
   boolean mode = false;
   if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
      // Android 4.2 (API level 17) onwards.
      mode = Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
   }
   else {
      // Android 4.1 (API level 16) and earlier.
      mode = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
   }
   return mode;
}

Did you note the “deprecation” and “NewApi” notations above? The “deprecation” notation indicates that the “Settings.System.AIRPLANE_MODE_ON” is deprecated as of Android API level 17 (codename Jellybean major release 1). Whilst the “NewApi” notation indicates that the constant “AIRPLANE_MODE_ON” has moved to the secured “Settings.Global” area of Android from API 17 onwards. Google cited security as the reason for the move. Really? The real reason may have less to do with security and more with tracking and ads distribution to mobile Android devices.

Since the constant was moved to the secured area of Android that only system applications or applications running on rooted devices can access, Android developers were no longer able to provide the same functionality on unrooted Android 4.2 (API level 17) plus mobile platforms. A common advice developers gave to their user base was “Root your device!“. Of course, not every user is equipped with the technical knowledge and skill to accomplish such task. Also, rooting an Android mobile device is not for the faint-hearted as users could potentially “brick” their Android mobile device and render it unusable—not to mention void the warranty of their mobile device with the manufacturer. So, despite attempts to inform their Android customers of the changes made by Google, many developers suddenly find themselves amidst shrinking sales and bad ratings. Once again, Android developers were being fleeced.

For those users who were lucky enough to have their Android mobile devices rooted, the airplane or flight mode function was again available via toggling of a button within an Android application. To toggle airplane or flight mode from Android 4.2 (API level 17) onwards on rooted Android devices:

private final String COMMAND_FLIGHT_MODE_1 = "settings put global airplane_mode_on";
private final String COMMAND_FLIGHT_MODE_2 = "am broadcast -a android.intent.action.AIRPLANE_MODE --ez state";

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static void setFlightMode(Context context) {
   if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
      // Android 4.2 (API level 17) onwards.
      if (isRooted(context)) {
         // Mobile devices with root or administrative privilege.
         int enabled = isFlightModeEnabled(context) ? 0 : 1;
         // Set airplane / flight mode using "su" commands.
         String command = COMMAND_FLIGHT_MODE_1 + " " + enabled;
         executeCommandViaSu(context, "-c", command);
         command = COMMAND_FLIGHT_MODE_2 + " " + enabled;
         executeCommandViaSu(context, "-c", command);
      }
      else {
         try {
            // No root or administrative privilege, just show the Airplane/Flight mode system setting.
            Intent intent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
         }
         catch (ActivityNotFoundException e) {
            Log.e(TAG, "Error due to: " + e.fillInStackTrace());
         }
      }
   }
}

Note: the isRooted method above is not listed here as Android developers have different ways of checking for root status of Android mobile devices.

To execute the commands via the “su” binary:

private static void executeCommandViaSu(Context context, String option, String command) {
   boolean success = false;
   String su = "su";
   for (int i = 0; i < 3; i++) {
      // "su" command executed successfully.
      if (success) {
         // Stop executing alternative su commands below.
         break;
      }
      if (i == 1) {
         su = "/system/xbin/su";
      }
      else if (i == 2) {
         su = "/system/bin/su";
      }
      try {
         // Execute command via "su".
         Runtime.getRuntime().exec(new String[]{su, option, command});
         success = true;
      }
      catch (IOException e) {
         success = false;
         Log.e(TAG, "Error due to: " + e.fillInStackTrace());
      }
   }
}

The commands above are relatively straightforward. The first command “settings put global airplane_mode_on” sets the constant “airplane_mode_on” by passing in a true or false state. Whilst the second command “am broadcast -a android.intent.action.AIRPLANE_MODE –ez state” fires off a broadcast via Android’s activity manager (am) with an intent to set the constant “airplane_mode_on” by passing in a true or false state. Of course, both commands need to be executed with root privilege via the “su” binary.

For users who can’t root their Android mobile devices, Android applications should redirect them to the system settings so users can manually toggle the airplane or flight mode function.

private static void showFlightModeSystemSetting(Context context) {
   try {
      Intent intent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(intent);
   }
   catch (ActivityNotFoundException e) {
      Log.e(TAG, "Error due to: " + e.fillInStackTrace());
   }
}

Of course, this workaround is not ideal, but at least users on Android 4.2 (API level 17) plus mobile devices can still activate the airplane or flight mode function—albeilt manually and with a few extra steps.

To date, Android 7—codename Nougat—has not provided a public API method to automatically toggle the airplane or flight mode function for non-rooted Android mobile devices. So, the Android platform has become increasingly more fragmented. Let’s hope Google can implement a public method soon! Else, Android developers might move en masse to the next competing mobile platform that is more open for development.

Copyright © 2016. All rights reserved.
Unauthorised reproduction, in whole or in parts, will be prosecuted to the maximum extent under law.

Android is a trademark of Google LLC.
Bluetooth word mark and logos are registered trademarks owned by Bluetooth SIG, Inc..

2 thoughts on “Android: How to activate the airplane or flight mode”

    1. Not with the “settings” or “am” command – there’s no timing input parameter. You might want to use a Handler.postDelayed() or an IntentService if you want to activate or deactivate the airplane mode with a timer.

      Like

Leave a comment