简体   繁体   English

通过 ADB 打开飞行模式

[英]Turning airplane mode on via ADB

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.我希望找到一种从 ADB 切换飞行模式的方法,该方法不以任何方式涉及 GUI。 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.我找到并使用了调用菜单然后使用keyevents打开飞行模式的方法,但是由于需要考虑到UI布局可能发生的变化,这种方法只是一个临时解决方案。

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.我开始认为没有办法通过意图打开或关闭飞行模式,但如果您有任何信息,我将不胜感激。

First, I would never rely on solutions which are blindly navigating through the UI as suggested in most answers here.首先,我永远不会依赖在大多数答案中建议的盲目浏览 UI的解决方案。 More in a note below.更多在下面的注释中。

I like the G murali's first answer which suggests the following to enable the airplane mode:我喜欢 Gmurali 的第一个答案,它建议以下启用飞行模式:

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:禁用飞行模式,您必须将设置更改为 0 并再次广播意图:

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 .它已经过测试并在 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.许多答案建议使用活动打开飞行模式设置,然后模拟一些按键以在 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.此外,不同品牌、型号、Android 版本等设备上的 UI 可能会有所不同。我已经在 Samsung Galaxy S4(Android 6.0.1)上对其进行了测试,但它在那里不起作用。 It may be by a difference between the Samsung's and the default UI.这可能是三星和默认用户界面之间的差异。

The settings manipulation method should be perfectly safe.设置操作方法应该是完全安全的。

For newer android this toggles WiFi.对于较新的 android,这会切换 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 .归功于这个答案

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

To turn-on APN mode打开 APN 模式

adb shell settings put global airplane_mode_on 1

To turn-off APN mode关闭 APN 模式

adb shell settings put global airplane_mode_on 0 

verified on Android-L在 Android-L 上验证

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

This is how I was able to enable Airplane mode on emulator (API 25) overcoming the permission issue.这就是我能够在模拟器(API 25)上启用飞行模式以克服权限问题的方式。

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.请注意,您的“ su 0 ”可能会有所不同。 For example, for older api I had to use ' su & '例如,对于较旧的 api,我必须使用 ' 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.但是,我看到过几秒钟后模拟器自动重新连接的情况。

only worked on Rooted device.仅适用于 Rooted 设备。 . .

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

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. adb shell settings get global airplane_mode_on对我不起作用,找不到设置。

In my .zshrc file I added this to simulate airplane mode.在我的 .zshrc 文件中,我添加了它来模拟飞行模式。

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

Recent Android versions with cmd:最近的 Android 版本与 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 

Use the following command to launch Settings > Wireless networks , toggle Airplane mode and return (by pressing Back):使用以下命令启动Settings > Wireless networks ,切换飞行模式并返回(按返回):

  • 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 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 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 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).在三星 Galaxy S (Android OS 2.3.3) 上测试。

william's answer was the closest one that worked for me -> Android 5.0 Lollipop.威廉的答案是最适合我的答案-> 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.所以我让终端休眠了 0.1 秒。 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";

Since the broadcast commands stopped working after Nougat, I would recommend using something like this:由于在 Nougat 之后广播命令停止工作,我建议使用如下内容:

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这适用于三星 S7

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使用 OS 5.x 6.x 7.x 和 8.x 需要针对 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.如果手机已root,则可以使用此命令。 Also if using Magisk, shell must be given "su" permission此外,如果使用 Magisk,shell 必须获得“su”权限

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'" adb -s ZY32234G3V shell "su -c 'input keyevent KEYCODE_WAKEUP;input keyevent KEYCODE_MOVE_HOME;settings put global plane_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'" adb -s ZY32234G3V shell "su -c 'input keyevent KEYCODE_WAKEUP;input keyevent KEYCODE_MOVE_HOME;settings put global plane_mode_on 0;am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false'"

If you have root access, and the method in the answer from David Ferenczy Rogožan left you with a security exception:如果您具有 root 访问权限,并且David Ferenczy Rogožan的回答中的方法给您留下了安全异常:

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:根据我对Android 9的经验,su 的显式调用仅对广播命令是必需的,而对于前者我可以保留:

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.感谢Arya将这个技巧留在上述答案的评论部分。

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.要关闭,只需在第一个命令中传递 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM