Android 在手机上更改设置或执行任务的 Adb shell 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2203495/
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 04:56:30  来源:igfitidea点击:

Adb shell commands to change settings or perform tasks on a phone

androidadb

提问by Noah

How do I use adb to perform some automated tasks on my android phone? I need to find commands that I can issue from the command line (ideally, using a .bat file) that will be capable of more than simply opening an application or sending an input keyevent (button press).

如何使用 adb 在我的 android 手机上执行一些自动化任务?我需要找到可以从命令行(理想情况下,使用 .bat 文件)发出的命令,这些命令不仅能够简单地打开应用程序或发送输入键事件(按下按钮)。

For instance, I want to toggle Airplane Mode on or off from the command line. Currently, the best I can do is launch the Wireless & network settings menu and then use input keyevents to click Airplane mode:

例如,我想从命令行打开或关闭飞行模式。目前,我能做的最好的事情是启动无线和网络设置菜单,然后使用输入键事件单击飞行模式:

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

There are quite a few drawbacks to this method, primarily that the screen has to be on and unlocked. Also, the tasks I want to do are much broader than this simple example. Other things I'd like to do if possible:

这种方法有很多缺点,主要是屏幕必须打开和解锁。此外,我想做的任务比这个简单的例子要广泛得多。如果可能的话,我想做的其他事情:

1.Play an mp3 and set it on repeat. Current solution:

1.播放 mp3 并将其设置为重复播放。当前解决方案:

adb shell am start -n com.android.music/.MusicBrowserActivity
adb shell input keyevent 84
adb shell input keyevent 56 & adb shell input keyevent 66 & adb shell input keyevent 67 & adb shell input keyevent 19
adb shell input keyevent 23 & adb shell input keyevent 21
adb shell input keyevent 19 & adb shell input keyevent 19 & adb shell input keyevent 19 & adb shell input keyevent 22 & adb shell input keyevent 22 & adb shell input keyevent 23 & adb shell input keyevent 23

2.Play a video. (current solution: open MediaGallery, send keyevents, similar to above)

2.播放视频。(当前解决方案:打开MediaGallery,发送keyevents,类似上面)

3.Change the volume (current solution: send volume-up button keyevents)

3.更改音量(当前解决方案:发送音量增大按钮按键事件)

4.Change the display timeout (current solution: open sound & display settings, send keyevents)

4.更改显示超时(当前解决方案:打开声音&显示设置,发送keyevents)

As before, these all require the screen to be on and unlocked. The other major drawback to using keyevents is if the UI of the application is changed, the keyevents will no longer perform the correct function. If there is no easier way to do these sort of things, is there at least a way to turn the screen on (using adb) when it is off? Or to have keyevents still work when the screen is off?

和以前一样,这些都需要打开和解锁屏幕。使用 keyevents 的另一个主要缺点是如果应用程序的 UI 发生变化,keyevents 将不再执行正确的功能。如果没有更简单的方法来做这些事情,是否至少有一种方法可以在屏幕关闭时打开(使用 adb)?或者在屏幕关闭时让按键事件仍然有效?

I'm not very familiar with java. That said, I've seen code like the following (source: http://yenliangl.blogspot.com/2009/12/toggle-airplane-mode.html) to change a setting on the phone:

我对java不是很熟悉。也就是说,我已经看到如下代码(来源:http: //yenliangl.blogspot.com/2009/12/toggle-airplane-mode.html)来更改手机上的设置:

Settings.System.putInt(Settings.System.AIRPLANE_MODE_ON, 1 /* 1 or 0 */);

How do I translate something like the above into an adb shell command? Is this possible, or the wrong way to think about it?

如何将上述内容转换为 adb shell 命令?这是可能的,还是错误的思考方式?

I can provide more details if needed. Thanks!

如果需要,我可以提供更多详细信息。谢谢!

回答by rohitverma

Although question is old, it might help someone else.

虽然问题很老,但它可能会帮助别人。

For video playback, you can try this:

对于视频播放,你可以试试这个:

adb shell am start -a android.intent.action.VIEW -d   "file:///mnt/sdcard/DCIM/Camera/test.3gp" -t "video/*" 

^gives you a prompt of all capableplayers that can play this file.

^ 提示您可以播放此文件的所有有能力的播放器。

adb shell am start -a android.intent.action.VIEW -d "file:///mnt/sdcard/DCIM/Camera/test.3gp" -t "video/*" -n "com.alensw.PicFolder/.PlayerActivity" 

^plays in player specified by switch -n.

^ 在由开关 -n 指定的播放器中播放。

回答by Bill

I'm working on the same set of issues. (I mostly solved the context issue with straight button presses by using the keyevent HOME and then MENU, but -- somehow -- even that's unreliable.) I'm currently investigating SL4A(Scripting Layer for Android), which has promise. It allows Perl, Python, Lua, and other scripting languages to interact with the Android API from your PC after starting an SL4A server on the device -- which can also be done from the PC. I'm finding "Pro Android Python with SL4A" to be an excellent resource; I would have saved myself days of trial-and-error and hunting on the Web if I had started with that book

我正在处理同一组问题。(我主要通过使用 keyevent HOME 和 MENU 解决了直接按下按钮的上下文问题,但是——不知何故——即使这样也不可靠。)我目前正在研究SL4A(Android 脚本层),它很有希望。它允许 Perl、Python、Lua 和其他脚本语言在设备上启动 SL4A 服务器后从您的 PC 与 Android API 交互——这也可以从 PC 完成。我发现“ Pro Android Python with SL4A”是一个很好的资源;如果我从那本书开始,我会省去几天的反复试验和网上搜索

回答by Maurits Rijk

The Java example you show is for a program that is running on the phone itself. You might be able to program some kind of an interpreter on the phone that handles adb commands. That way you are not dependent anymore on keyevents. This is not a minor undertaking, however.

您展示的 Java 示例适用于在手机上运行的程序。您或许可以在手机上编写某种解释器来处理 adb 命令。这样你就不再依赖于关键事件。然而,这不是一件小事。

回答by Bill

MonkeyRunneralso looks like it has promise, though I haven't dug deep enough to find its limits yet.

MonkeyRunner看起来也很有希望,尽管我还没有深入挖掘到它的极限。

Android ScreenCastlets you view and control your device's screen from your PC, which also has potential for automation. It does have present some logistical issues for that application, though.

Android ScreenCast可让您从 PC 上查看和控制设备的屏幕,这也具有自动化的潜力。不过,它确实为该应用程序带来了一些后勤问题。

By the way, you can get past the locked homescreen with a MENU keyevent and a set of sendevents (in place of keyevents).

顺便说一句,您可以使用 MENU 键事件和一组发送事件(代替键事件)来绕过锁定的主屏幕。