上一篇使用java调用monkeyrunner(http://www.cnblogs.com/nuliniaoboke/archive/2012/11/23/2784385.html)中遗留了一个问题,就是上次用的是低版本的4个包解决的问题,使用高版本的jar包怎么调用monkeyrunner呢? 经过一位朋友的提示说,现在高版本的方法已经变了,我就按照他的提示,上网搜了一下需要的类,测试通过后,特写此补充篇总结一下。 上次使用的是android sdk tools路径下的lib里面的4个包:ddmlib.jar,guavalib.jar,monkeyrunner.jar,sdklib.jar.而更新后的版本需要添加另外一个包就是:chimpchat.jar,monkerunner.jar这个包倒不是必须的了。另外,低版本中是用AdbMonkeyDevice实现IMonkeyDevice,高版本中没有这两个类了,用的AdbChimpDevice和IchimpDevice。 而通过查看AdbChimpDevice( http://code.google.com/p/aster/source/browse/src/com/android/chimpchat/adb/AdbChimpDevice.java?r=967f7f8cd6249c69e00c6de7ff1b55bd0f51d311 )和IchimpDevice( http://code.google.com/p/aster/source/browse/src/com/android/chimpchat/core/IChimpDevice.java?r=967f7f8cd6249c69e00c6de7ff1b55bd0f51d311 )这两个类在官方的源码,就不难发现,AdbChimpDevice实现了IchimpDevice这个接口,不过连接方法还是通过AdbBackend,通过adb方式连接模拟器,或者真机。只是Device的父类发生了变化。 下面还是用以前的测试类,进行稍微改变一下,就可以看出两者的不同: import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import com.android.chimpchat.adb.AdbBackend; import com.android.chimpchat.adb.AdbChimpDevice; public class TestNewMonkeyrunner { /** * @param args */ //这里有变化 private static AdbChimpDevice device; private static AdbBackend adb; public static void main(String[] args) { // TODO Auto-generated method stub if (adb==null){ adb = new AdbBackend(); // 参数分别为自己定义的等待连接时间和设备id //这里需要注意一下adb的类型 device = (AdbChimpDevice) adb.waitForConnection(8000,"MSM8225QRD5"); } //添加启动权限 String action = "android.intent.action.MAIN"; Collection<String> categories = new ArrayList<String>(); categories.add("android.intent.category.LAUNCHER"); // 启动要测试的主界面 device.startActivity(null, action, null, null, categories, new HashMap<String, Object>(),"cn.com.fetion/.android.ui.activities.StartActivity", 0); // 点击某一个坐标 //touch方法略有变化 device.touch(202,258,com.android.chimpchat.core.TouchPressType.DOWN_AND_UP); } } 从上面可以看出,高版本与低版本的变化,并不是很多。只要连接上设备,一些需要用到的操作方法,自己可以去源码里面看,也可以自己重写一些常用的方法。 源码里的注释是非常详细,比如IchimpDevice接口类中的startActivity方法: void startActivity(@Nullable String uri, @Nullable String action, @Nullable String data, @Nullable String mimeType, Collection<String> categories, Map<String, Object> extras, @Nullable String component, int flags); /** * Send a broadcast intent to the device. * * @param uri the URI for the Intent * @param action the action for the Intent * @param data the data URI for the Intent * @param mimeType the mime type for the Intent * @param categories the category names for the Intent * @param extras the extras to add to the Intent * @param component the component of the Intent * @param flags the flags for the Intent */ 对各个参数解释的都很清楚。所以,建议在研究java调用monkeyrunner问题的朋友们,不要忘了源码这个最好的资源。 追Windows8应用浪潮,做微软首批开发者 |
|