android开发免不了要用到android
,你可以在系统环境变量里面配置一个ANDROID_HOME变量,把你的android
C:\Users\curryl\adt-bundle-windows-x86_64-20140702\sdk\tools
然后随便打开一个cmd命令行窗口,
这时候你需要打开一个模拟器或者连接你的真实设备到电脑上。
1.模拟器:
C:\Users\curryl\adt-bundle-windows-x86_64-20140702\sdk\tools>emulator -avd AVD2.3.3
Comments: 定位到android SDK的路径下的tools目录,运行上面这个命令。“AVD2.3.3”是我的模拟器的名字,这个命令是每个人自己命名的,用的时候需改成实际的名字。
2.实际设备:随便打开一个cmd,运行adb devices,
C:\Users\curryl>adb devices
List of devices attached
05131138014091062F69
使用:
在输入 monkeyrunner之后出现shell 交互界面,
输入类似下面的 语句或(当然,我们也可以不这样麻烦得一行行地输入命令,可以将从“from...import...”开始的所有后面的语句都写到一个python文件里面,例如 monkeyrunnerTest.py,然后我们再从命令行直接通过monkeyrunner运行它即可。比如,我们还是用上面的例子,语法如下:
C:\Users\curryl\adt-bundle-windows-x86_64-20140702\sdk\tools>monkeyrunner monkeyrunnerTest.py
接下来monkeyrunner会自动调用monkeyrunnerTest.py,并执行其中的语句,相当方便。)
- #
Imports the monkeyrunner modules used by this program - from
com.android.monkeyrunner import MonkeyRunner, MonkeyDevice -
- #
Connects to the current device, returning a MonkeyDevice object - device
= MonkeyRunner.waitForConnection() -
- #
Installs the Android package. Notice that this method returns a boolean, - #
so you can test to see if the installation worked. - device.installPackage('myproject/bin/MyApplication.apk')
-
- #
sets a variable with the package's internal name - package
= 'com.example.android.myapplication' -
- #
sets a variable with the name of an Activity in the package - activity
= 'com.example.android.myapplication.MainActivity' -
- #
sets the name of the component to start - runComponent
= package + '/' + activity -
- #
Runs the component - device.startActivity(component=runComponent)
-
- #
Presses the Menu button - device.press('KEYCODE_MENU','DOWN_AND_UP')
-
- #
Takes a screenshot - result
= device.takeSnapshot() -
- #
Writes the screenshot to a file - result.writeToFile('myproject/shot1.png','png')
如果是实际手机的话,可以输入刚刚查询到的设备号。
device=MonkeyRunner.waitForConnection(5,'05131138014091062F69')
C:\Users\curryl\adt-bundle-windows-x86_64-20140702\sdk\tools>monkeyrunner
Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:54:35)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_25
>>> from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
>>> device=MonkeyRunner.waitForConnection(5,'05131138014091062F69')
>>> device.press('KEYCODE_MENU','DOWN_AND_UP')
再看看录制回放功能:
详见
这个缺点是只能通过电脑上的屏幕操作,录制太慢了。