简体   繁体   English

如何使用 adb 在 android 上启用飞行模式

[英]How to enable airplane mode on android using adb

I have an android phone on which I want to programatically toggle airplane mode.我有一部 android 手机,我想在其上以编程方式切换飞行模式。

I've been unable to root this device, which has prevented any apps I'm running from having permission to edit these settings, so am using adb to open the settings menu and toggle the switch there.我一直无法root此设备,这阻止了我正在运行的任何应用程序有权编辑这些设置,所以我使用adb打开设置菜单并在那里切换开关。

Executing the following adb shell commands will work in some cases:在某些情况下,执行以下 adb shell 命令会起作用:

//Get airplane mode settings menu
adb -s ${device} shell am start -a android.settings.AIRPLANE_MODE_SETTINGS

//Hit enter (as many times as required)
adb -s ${device} shell input keyevent KEYCODE_ENTER

//Check the state of airplane mode to determine when successful
adb -s ${device} shell settings get global airplane_mode_on

However, this only work if the menu containing the airplane mode switch is the first menu option listed on the settings page, since "android.settings.AIRPLANE_MODE_SETTINGS" will only navigate to the menu where the setting is found.但是,这仅在包含飞行模式开关的菜单是设置页面上列出的第一个菜单选项时才有效,因为“android.settings.AIRPLANE_MODE_SETTINGS”只会导航到找到设置的菜单。 It will not perform any focussing on the setting itself, so the following command to hit enter will press the wrong setting.它不会对设置本身执行任何聚焦,因此以下按 Enter 的命令将按错误的设置。

If using this approach, is there a way to identify/focus only the airplane toggle switch, or otherwise check which menu item is in focus so the items can be iterated until the correct one is found?如果使用这种方法,有没有办法只识别/聚焦飞机拨动开关,或者检查哪个菜单项处于焦点状态,以便可以迭代这些项目直到找到正确的项目?

Additionally, it would be great if there was a way to execute these adb commands without requiring the phone to be plugged into a PC - ie can the adb client be run on the phone itself (and then trick itself into thinking it is a connected device).此外,如果有一种方法可以在不需要将手机插入 PC 的情况下执行这些 adb 命令,那就太好了 - 即 adb 客户端可以在手机本身上运行(然后欺骗自己认为它是一个连接的设备)。

And finally - the end goal here is simply to toggle airplane mode programatically on demand (ultimately the request will be triggered by a node.js process running in termux app).最后 - 这里的最终目标只是按需以编程方式切换飞行模式(最终请求将由运行在 termux 应用程序中的 node.js 进程触发)。 If there is a better way to achieve this without using adb that doesn't require root permissions, this would also work.如果有更好的方法可以在不使用不需要 root 权限的 adb 的情况下实现这一点,这也可以。

Here are the ADB commands to turn on and turn of airplane mode:以下是打开和打开飞行模式的 ADB 命令:

Turn on:打开:

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

Turn off:关掉:

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

I suspect this is an incredibly niche requirement, but if anyone is interested in how to toggle airplane mode on a non-rooted android phone using adb commands, where the UI airplane mode toggle button is not the first item in the settings activity.... read below!我怀疑这是一个令人难以置信的利基要求,但如果有人对如何使用 adb 命令在无根 android 手机上切换飞行模式感兴趣,其中 UI 飞行模式切换按钮不是设置活动中的第一项...... 。 参见下文!

As previously mentioned, load the screen containing the airplane mode button:如前所述,加载包含飞行模式按钮的屏幕:

adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS

Now to check the current state of the UI:现在查看当前的 state 的 UI:

adb shell uiautomator dump
adb pull /sdcard/window_dump.xml

STDOUT of the first command above can be checked to determine exactly where the uiautomator dump is sent by default.可以检查上面第一个命令的 STDOUT 以确定默认情况下将 uiautomator 转储发送到何处。

To navigate the settings menu, use the DPAD UP/DOWN keyevents:要导航设置菜单,请使用 DPAD UP/DOWN 键事件:

adb shell input keyevent KEYCODE_DPAD_DOWN
adb shell input keyevent KEYCODE_DPAD_UP

Depending on the device, I've found that using the above options either results in nodes being selected or focused.根据设备的不同,我发现使用上述选项会导致节点被选中或被聚焦。 The output of the UI dump can be parsed, and in the DOM search for either "node[selected=true]" or "node[focused=true]" depending on which the device uses.可以解析 UI 转储的 output,并在 DOM 中搜索“node[selected=true]”或“node[focused=true]”,具体取决于设备使用的情况。 I'm eventually checking to see when node.attr('text').toUpperCase.contains('PLANE MODE') to check when the airplane mode setting has been reached, and then finally executing:我最终会检查node.attr('text').toUpperCase.contains('PLANE MODE')何时检查何时达到飞行模式设置,然后最终执行:

adb shell input keyevent KEYCODE_ENTER

to activate the switch.激活开关。

For now this covers the devices I've tried it on, but performance is not great if there is a lot of scrolling in the menu to do.目前,这涵盖了我尝试过的设备,但如果菜单中有很多滚动操作,性能就不是很好。 I'd be interested to know if there's a way to hold uiautomator dump output in memory on the device and parse the XML there to avoid unnecessary I/O of outputting to a file, pulling to a local drive and reading it back into memory there. I'd be interested to know if there's a way to hold uiautomator dump output in memory on the device and parse the XML there to avoid unnecessary I/O of outputting to a file, pulling to a local drive and reading it back into memory there .

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 

Create bat file:创建bat文件:

adb -s 3e31d8d97d95 shell am start -a android.settings.AIRPLANE_MODE_SETTINGS; adb -s 3e31d8d97d95 shell 上午开始 -a android.settings.AIRPLANE_MODE_SETTINGS; while [ 0 -le 5 ];而 [ 0 -le 5 ]; do input keyevent 23;输入keyevent 23; sleep 0.1;input keyevent 23;sleep 13; sleep 0.1;输入keyevent 23;sleep 13; echo 'done';回声“完成”; done;完毕;

Make sure the "stay awake" is ON in debug menu.确保调试菜单中的“保持清醒”处于打开状态。 Also make sure this is more "gray" - focues:还要确保这更“灰色” - 重点: 在此处输入图像描述

Run the script.运行脚本。

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

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