android – Turning airplane mode on via ADB-ThrowExceptions

Exception or error:

I’ve looked all over and I haven’t found a concrete answer to this question. I’m hoping to find a way to toggle airplane mode from ADB that doesn’t involve the GUI in any way. I’ve found and used the method that calls up the menu and then using keyevents turns airplane mode on, but due to needing to account for possible changes in UI layout, this method is only a temporary solution.

I’m beginning to think there isn’t a way to just toggle airplane mode on or off via intent, but if you have any information, I’d greatly appreciate it.

How to solve:

First, I would never rely on solutions which are blindly navigating through the UI as suggested in most answers here. More in a note below.

I like the G murali’s first answer which suggests the following to enable the airplane mode:

adb shell settings put global airplane_mode_on 1

Unfortunately, it doesn’t work. It just changes the state of the Airplane mode icon, but all radios are still active.

It’s missing one very important part, which is broadcasting the intent right after the setting has been changed to inform applications that the Airplane mode state has changed.

To enable the Airplane mode use the following commands:

adb shell settings put global airplane_mode_on 1
adb shell am broadcast -a android.intent.action.AIRPLANE_MODE

To disable the Airplane mode, you have to chnage the setting to 0 and broadcast the intent again:

adb shell settings put global airplane_mode_on 0
adb shell am broadcast -a android.intent.action.AIRPLANE_MODE

It’s tested and working on Android 6.


Note

Many answers suggest to use an activity to open the Airplane mode settings and then simulate some key presses to navigate through the UI. I don’t recommend to rely on that. There may be just a short lag on the device and keys will be pressed before the Settings dialog is opened or for example some unexpected dialog will appear meanwhile. In both cases the key presses will cause something unintended, possibly harmful.

Also the UI may be different on devices of various brands, models, Android versions, etc. I have tested it on Samsung Galaxy S4 (Android 6.0.1) and it didn’t work there. It may be by a difference between the Samsung’s and the default UI.

The settings manipulation method should be perfectly safe.

###

To turn-on APN mode

adb shell settings put global airplane_mode_on 1

To turn-off APN mode

adb shell settings put global airplane_mode_on 0 

verified on Android-L

###

For newer android this en/disables wifi. Not quite your task, but i would’ve been happy to find it here:

adb shell svc wifi disable

or

adb shell svc wifi enable

Credit goes to this answer: https://stackoverflow.com/a/27228962/1021872

###

adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS
adb shell input keyevent 19 & adb shell input keyevent 23

###

This is how I was able to enable Airplane mode on emulator (API 25) overcoming the permission issue.

adb -s emulator-5554 shell settings put global airplane_mode_on 1
adb -s emulator-5554 shell 'su 0 am broadcast -a android.intent.action.AIRPLANE_MODE'

Please note that your ‘su 0‘ may vary. For example, for older api I had to use ‘su &

If in case you are looking to disable just Data on emulator, use

svc data disable

However, I have seen cases where emulator auto reconnects after a few seconds.

###

adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS & adb shell input keyevent KEYCODE_ENTER & adb shell input keyevent 4

Will toggle airplane mode. However does anyone know if there’s a way to explicitly uncheck or check the box? The problem is I can’t find a way to check the state before toggling. adb shell settings get global airplane_mode_on does not work for me, settings is not found.

###

turn off Airplane mode :

if [[ "$(adb shell dumpsys wifi | grep mAirplaneModeOn)" == "mAirplaneModeOn true" ]]; then  

    adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS
        && input keyevent 19
        && input keyevent 23
        && input keyevent 4
fi

turn on Airplane mode :

if [[ "$(adb shell dumpsys wifi | grep mAirplaneModeOn)" == "mAirplaneModeOn false" ]]; then            

    adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS
        && input keyevent 19
        && input keyevent 23
        && input keyevent 4
fi

###

only worked on Rooted device..

turn onAirplane mode :

adb shell -c settings put global airplane_mode_on 1
adb shell -c am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true

turn off Airplane mode :

adb shell -c settings put global airplane_mode_on 0
adb shell -c am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false

###

Use the following command to launch Settings > Wireless networks, toggle Airplane mode and return (by pressing Back):

  • Windows/DOS:
    adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS & adb shell input keyevent 19 & adb shell input keyevent 23 & adb shell input keyevent 4
  • Unix/Linux:
    adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS ; adb shell input keyevent 19 ; adb shell input keyevent 23 ; adb shell input keyevent 4

Tested on Samsung Galaxy S (Android OS 2.3.3).

###

william’s answer was the closest one that worked for me -> Android 5.0 Lollipop.
However, sometimes it wouldn’t toggle because it was trying to toggle before the Settings activity was fully loaded.

So I made the terminal sleep for 0.1 seconds. and I added hitting the back to exit out of Settings

adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS
adb shell input keyevent 19 ; sleep 0.1; adb shell input keyevent 23; sleep 0.3; adb shell input keyevent 4;
echo "Toggled airplane mode";

###

A working method which I am using in my Mobile Automation Framework:

Worked with OS 5.x 6.x 7.x and 8.x need to test it against 9.x

/**
 * Method to set Android device Airplane mode.
 * 
 * @param status
 * 
 *               true - turn on airplane mode false - turn off airplane mode
 */
public static void setAndroidDeviceAirplaneMode(boolean status) {
    try {

        String airplaneModeStatus = "";
        if (status) {
            airplaneModeStatus = "0";
        } else {
            airplaneModeStatus = "0";
        }
        String sdkPath = System.getenv("ANDROID_HOME") + "/platform-tools/";
        Runtime.getRuntime().exec(sdkPath + "adb shell settings put global airplane_mode_on " + airplaneModeStatus);
        Thread.sleep(1000);
        Process process = Runtime.getRuntime()
                .exec(sdkPath + "adb shell am broadcast -a android.intent.action.AIRPLANE_MODE");
        process.waitFor();
        Thread.sleep(4000);
        if (status) {
            logger.info("Android device Airplane mode status is set to ON");
        } else {
            logger.info("Android device Airplane mode status is set to OFF");
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
        logger.error("Unable to set android device Airplane mode.");
    }
}

###

If the phone is rooted then you can use this command. Also if using Magisk, shell must be given “su” permission

Enable flight mode

adb -s ZY32234G3V shell “su -c ‘input keyevent KEYCODE_WAKEUP;input keyevent KEYCODE_MOVE_HOME;settings put global airplane_mode_on 1;am broadcast -a android.intent.action.AIRPLANE_MODE –ez state true'”

Disable flight mode

adb -s ZY32234G3V shell “su -c ‘input keyevent KEYCODE_WAKEUP;input keyevent KEYCODE_MOVE_HOME;settings put global airplane_mode_on 0;am broadcast -a android.intent.action.AIRPLANE_MODE –ez state false'”

###

Since the broadcast commands stopped working after Nougat, I would recommend using something like this:

adb shell "input keyevent KEYCODE_WAKEUP;input keyevent KEYCODE_MOVE_HOME;am start -a android.settings.AIRPLANE_MODE_SETTINGS;sleep 0.5;input keyevent KEYCODE_ENTER;pm clear com.android.settings;input keyevent KEYCODE_HOME"

This works on Samsung S7

###

In my .zshrc file I added this to simulate airplane mode.

alias android:online="androidOfflineOnline enable"
alias android:offline="androidOfflineOnline disable"
function androidOfflineOnline {
   adb shell svc data $1
   adb shell svc wifi $1
}

Leave a Reply

Your email address will not be published. Required fields are marked *