上一篇使用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問題的朋友們,不要忘了源碼這個最好的資源。
首發地址:http://www.eoeandroid.com/thread-232303-1-1.html