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 an
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
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 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.
adb shell "su -c 'settings put global airplane_mode_on 1'"
adb shell "su -c 'am broadcast -a android.intent.action.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.
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";
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.
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