1 # Imports the monkeyrunner modules used by this program 2 3 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage 4 5 # Connects to the current device, returning a MonkeyDevice object 6 7 device = MonkeyRunner.waitForConnection() 8 9 # Installs the Android package. Notice that this method returns a boolean, so you can test10 11 # to see if the installation worked.12 13 device.installPackage('./ApiDemos.apk')14 15 16 # Runs the component17 18 device.startActivity(component='com.example.android.apis/.ApiDemos')19 20 21 # Presses the Menu button22 23 device.press('KEYCODE_MENU','DOWN_AND_UP')24 25 26 # Takes a screenshot27 28 result = device.takeSnapshot()29 30 31 # Writes the screenshot to a file32 33 result.writeToFile('./shot1.png','png')复制代码
注意:SDK上的例子有些错误,不可直接复制,否则执行命令时会发生错误。具体可与我的上面这段代码对照。
3、 打开命令行转到Android_Root\tools目录下运行一下命令:
monkeyrunner monkeyrunnerprogram.py
110307 15:33:19.625:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.
110307 15:33:20.625:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.
110307 15:33:21.625:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.