Android 通过 ADB 打开飞行模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10506591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:58:40  来源:igfitidea点击:

Turning airplane mode on via ADB

android

提问by Charles Daily

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.

我已经看遍了,我还没有找到这个问题的具体答案。我希望找到一种从 ADB 切换飞行模式的方法,它不以任何方式涉及 GUI。我发现并使用了调用菜单然后使用 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.

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

回答by David Ferenczy Rogo?an

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

首先,我永远不会依赖于大多数答案中建议的盲目浏览 UI 的解决方案。更多在下面的注释中。

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

我喜欢 G murali 的第一个答案,它建议使用以下方法启用飞行模式:

adb shell settings put global airplane_mode_on 1

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

不幸的是,它不起作用。它只是改变飞行模式图标的状态,但所有无线电仍然处于活动状态

It's missing one very important part, which is broadcasting the intentright after the setting has been changed to inform applications that the Airplane modestate has changed.

它缺少一个非常重要的部分,即在更改设置后立即广播意图,以通知应用程序飞行模式状态已更改。

To enable the Airplane modeuse 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. 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.

许多答案建议使用活动来打开飞行模式设置,然后模拟一些按键操作以在 UI 中导航。我不建议依赖那个。设备上可能只有短暂的延迟,在打开设置对话框之前按键会被按下,或者例如同时出现一些意外的对话框。在这两种情况下,按键都会导致一些意想不到的事情,可能是有害的。

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.

此外,不同品牌、型号、Android 版本等的设备的 UI 可能会有所不同。我已经在三星 Galaxy S4 (Android 6.0.1) 上对其进行了测试,但在那里不起作用。这可能是三星和默认用户界面之间的差异。

The settings manipulation method should be perfectly safe.

设置操作方法应该是完全安全的。

回答by G murali Madhav

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 上验证

回答by cdanzmann

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

对于较新的 android,这将启用/禁用 wifi。不是你的任务,但我很乐意在这里找到它:

adb shell svc wifi disable

or

或者

adb shell svc wifi enable

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

归功于此答案:https: //stackoverflow.com/a/27228962/1021872

回答by AccTest

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. For example, for older api I had to use 'su &'

请注意,您的“ su 0”可能会有所不同。例如,对于较旧的 api,我必须使用 ' su &'

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

如果您只想禁用模拟器上的数据,请使用

svc data disable

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

但是,我见过模拟器在几秒钟后自动重新连接的情况。

回答by william

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

回答by JonFitt

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

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_ondoes not work for me, settings is not found.

将切换飞行模式。但是,有谁知道是否有办法明确取消选中或选中该框?问题是我找不到在切换之前检查状态的方法。adb shell settings get global airplane_mode_on对我不起作用,找不到设置。

回答by G murali Madhav

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

回答by ashish chauhan

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

回答by bboyairwreck

william's answerwas 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.

威廉的答案是最接近我的答案-> Android 5.0 Lollipop。但是,有时它不会切换,因为它试图在“设置”活动完全加载之前切换。

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

所以我让终端休眠了 0.1 秒。我添加了点击背部退出设置

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

回答by Juuso Ohtonen

Use the following command to launch Settings> Wireless networks, toggle Airplane modeand 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
  • 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
  • 视窗/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).

在三星 Galaxy S(Android 操作系统 2.3.3)上测试。