tags:

views:

1650

answers:

2

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).

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:

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)

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

4.Change the display timeout (current solution: open sound & display settings, send 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?

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:

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?

I can provide more details if needed. Thanks!

A: 

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.

Maurits Rijk
A: 

Maurits, Unfortunately, I'm no java programmer. I'm not looking to program an app to then send to the phone and run. I'm looking for a way to use adb to interface with a phone and get it to do simple things like change settings. Perhaps what I'm looking for is not possible or does not exist, however, it certainly seems like the possibility is there with adb and in particular "adb shell am". I'm hoping there is some more functionality that I'm just not aware of or some other way to accomplish what I want to do that I'm overlooking.

Noah
I'm afraid that is not possible with adb. What you are doing is already pretty ingenious :)
Maurits Rijk