33

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.

Charles Daily
  • 345
  • 1
  • 3
  • 6
  • You can check this link http://stackoverflow.com/questions/20130530/use-adb-to-check-if-airplane-mode-is-turned-on/26906472#26906472 – Anand Apr 14 '15 at 11:09
  • 3
    Could you accept any of answers Charles, please (I guess G's one is the best)? It'll help others to find the answer faster and also has some other consequences (like, for example, you can use this question as a duplicate of another one if it has the accepted answer). – David Ferenczy Rogožan Oct 21 '16 at 16:13
  • For recent devices e.g. Android 13, see the answer by wuseman with `adb shell cmd connectivity airplane-mode enable` – nealmcb Aug 23 '23 at 04:02

18 Answers18

60

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.

---UPDATE October 2022 If you got error about security settings, best solution now is: For mobile data: adb shell svc data disable For wifi: adb shell svc wifi disable

Community
  • 1
  • 1
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
  • 24
    This doesnt work anymore, "Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE" – Greg Ennis May 20 '17 at 13:27
  • @GregEnnis Would be good to specify on what set up it's not working anymore, otherwise it's useless information. – David Ferenczy Rogožan May 24 '17 at 15:49
  • Great solution, though doesn't work for me on Android 4.4.2 – mafonya May 25 '17 at 13:51
  • 5
    @DawidFerenczy I get 'Permission Denial - not allowed to send broadcast' on Android 7.0 and above. Tested on a Samsung A5 and S8 – Jack Dewhurst Jan 16 '18 at 09:22
  • My phone is rooted, any idea how to run those commands as root? – Arya Apr 10 '18 at 02:38
  • 3
    I found out, if your phone is rooted you can run $ adb shell "su -c 'settings put global airplane_mode_on 1'" $ adb shell "su -c 'am broadcast -a android.intent.action.AIRPLANE_MODE'" – Arya Apr 10 '18 at 02:41
  • public static void testtesttestairplane() { try { Process su = Runtime.getRuntime().exec("su"); DataOutputStream outputStream = new DataOutputStream(su.getOutputStream()); outputStream.writeBytes("settings put global airplane_mode_on 0"); outputStream.writeBytes("am broadcast -a android.intent.action.AIRPLANE_MODE"); outputStream.flush(); outputStream.writeBytes("exit\n"); outputStream.flush(); su.wait(2000); } catch (Exception e) { – zzz Nov 21 '18 at 12:17
  • This adb command works from command line, but when i am trying to execute same commands from espresso it doesn't work. above is the method which i am using in espresso . i have used same method for changing time,date etc ..and that works – zzz Nov 21 '18 at 12:21
  • @TanajiPadwal Could you just show the difference, please? It's not possible to format code in a comment properly. Or do you mean you're running it without `adb` command? If yes, then it's pointless, unfortunately. First, OP is asking how to achieve it using `adb` and second, it may work on Android without the `adb` command, but definitely not from the "outside" of a phone. – David Ferenczy Rogožan Nov 29 '18 at 01:26
  • @DawidFerenczy I was trying with the adb commands which are given above, but it did not worked , when i tried from espresso. i got the reference from other link and it worked ... solution which worked for me is by using connectivity manager .. please refer this answer https://stackoverflow.com/questions/52878138/unable-to-toggle-airplane-mode-espresso – zzz Nov 29 '18 at 12:14
  • @TanajiPadwal OK, once more: your case is completely different. Since you're running directly on a phone/emulator, you don't need to use `adb` at all. The OP was asking about controlling the airplane mode using the `adb` from outside of a phone. In other words: he's asking how to do it using `adb`, your answer is "don't use `adb`". – David Ferenczy Rogožan Nov 29 '18 at 19:19
  • @DavidFerenczyRogožan: I love your solution, however, it doesn't work on Android 10. I'm having the same issue when trying to execute the broadcasting the intern right after turn on airplane mode. It is complaining about permission issue. Do you have any suggestion on how to solve it or over come it ? Thanks – Hoang Minh Mar 24 '20 at 18:27
  • Worked for Me on HUAWEI LLD-AL10 (Android 9) – Banee Ishaque K Jan 17 '21 at 18:06
  • I did a working implementation of @zzz that display error message in the console https://github.com/bormat/ip_looper/blob/317175b3cd4c85a78b9300bfb1934cfd8fd7def7/app/src/main/java/com/example/iplooper/FirstFragment.java#L106 – bormat Mar 31 '21 at 22:21
  • If you are on a non-rooted device, you can always `adb reboot` instead of broadcasting the intent. This may not be what you want, given what you are doing/testing. – tir38 Aug 09 '22 at 15:22
  • For me it works fine, but for some reason the icon of airplane mode (on the status bar) isn't shown. Any idea why? – android developer Dec 24 '22 at 09:17
17

Recent Android versions with cmd:

Get airplane mode

adb shell cmd connectivity airplane-mode                                                  
disabled

Turn airplane mode enable/disable

adb shell cmd connectivity airplane-mode enable                     
adb shell cmd connectivity airplane-mode disable 
wuseman
  • 1,259
  • 12
  • 20
12

For newer android this toggles WiFi. Not quite your task, but I would've been happy to find it here:

adb shell svc wifi disabled

or

adb shell svc wifi enabled

Credit goes to this answer.

user598527
  • 175
  • 13
cdanzmann
  • 353
  • 3
  • 11
5

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.

AccTest
  • 313
  • 3
  • 11
  • 1
    How to find the correct 'su' ? I've tried both 'su 0' and 'su &' and they do not work. – Hoang Minh Mar 24 '20 at 18:28
  • 1
    The `su` command only works on [rooted devices](https://android.stackexchange.com/questions/127825/adb-shell-su-not-found). – Lafftar Apr 14 '22 at 14:11
  • The ^ commands worked for me when using an emulator. The only extra thing I need to do for it to take affect, was to restart the emulator ("adb reboot bootloader"). Then everything worked as expected – Austin Sep 27 '22 at 03:16
4
adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS
adb shell input keyevent 19 & adb shell input keyevent 23
slhck
  • 36,575
  • 28
  • 148
  • 201
william
  • 41
  • 2
  • am i correct to assume you missed a semi-colon before the second adb invocation? do you really want to run both keyevents in parallel? this isn't working for me, "InputDispatcher: Dropped event because input dispatch is disabled." – Michael Aug 10 '13 at 01:35
  • Worked on 2.3.3, but without running them in parallel. So replace the `&` with a semicolon `;` or have them in separate lines. It also works without the keyevent 19 – polym May 21 '15 at 17:16
4

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

G murali Madhav
  • 171
  • 3
  • 11
4

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
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
G murali Madhav
  • 171
  • 3
  • 11
4

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
ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
4

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
}
Stuart
  • 1,544
  • 5
  • 29
  • 45
3

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.

JonFitt
  • 303
  • 2
  • 9
2

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

ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
1

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).

Juuso Ohtonen
  • 8,826
  • 9
  • 65
  • 98
1

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";
Community
  • 1
  • 1
bboyairwreck
  • 331
  • 3
  • 6
  • This is really terrible because it's dependent on the UI and its behavior. A small lag occurs or an unexpected dialog appears and you're screwed (or your device is screwed). I would never rely on that. There are cleaner ways to achieve that. – David Ferenczy Rogožan Feb 16 '17 at 15:41
1

Here's what works for me:

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

To turn off, just pass 0 in the first command.

Flávio Faria
  • 6,575
  • 3
  • 39
  • 59
0

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.");
    }
}
0

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'"

0

If you have root access, and the method in the answer from David Ferenczy Rogožan left you with a security exception:

Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE

you can try to run the commands as superuser as follows.

Enable Airplane Mode

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

Disable Airplane Mode

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

In my experience with Android 9 the explicit invoke of su was only necessary for the broadcasting command while for the former I could keep:

adb shell settings put global airplane_mode_on [0|1]

Credits to Arya who left this trick into the comment section of the answer mentioned above.

DarioP
  • 5,377
  • 1
  • 33
  • 52
0

Here's what works for me:

To enable the Airplane mode:

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

To disable the Airplane mode:

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