再发一次,java怎么调用android中的monkeyrunner

 我来答
黑屋的寂寞
2015-12-04 · TA获得超过3339个赞
知道小有建树答主
回答量:747
采纳率:82%
帮助的人:352万
展开全部

若使用的是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问题的时候,不要忘了源码这个最好的资源。

受司大人
2015-01-29 · 知道合伙人影视综艺行家
受司大人
知道合伙人影视综艺行家
采纳数:20249 获赞数:171851
毕业于浙江广厦,有一定的电脑专业基础和两年工作经验,读过相关书籍多本

向TA提问 私信TA
展开全部
司项目,只能给你部分代码。你参考一下吧。
主要关注IMonkeyDevice ,AdbBackend ,你要的截图就是
IMonkeyImage newimage=device.takeSnapshot().getSubImage(x, y, width, height);
[mw_shl_code=java,true]package ei.workshop.monkey.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import com.android.monkeyrunner.adb.AdbBackend;
import com.android.monkeyrunner.core.IMonkeyDevice;
import com.android.monkeyrunner.core.IMonkeyImage;
import com.android.monkeyrunner.core.TouchPressType;

/**
* MonkeyRunner的运行代理类,相关方法
* @see #connect(String)
* @see #touchDown(int, int)
* @see #touchUp(int, int)
* @see #type(String)
* @see #press(String)
* @see #startActivity(String)
* @see #drag(int, int, int, int)
* @see #dispose()
* @author "laladin.syd"
*
*/
public class RunnerProxy {
private IMonkeyDevice device;
private static AdbBackend adb;
/**
* 定义特征库
*/
public static IMonkeyImage[] featuresArr=new IMonkeyImage[10];

public RunnerProxy() {
if (adb==null){
adb = new AdbBackend();
}
}

/**
* 连接设备,
* @param deviceID 超时时间,应该尽可能短
* @return 是否连接成功
*/
public boolean connect(String deviceID) {
if (adb != null){
System.out.println("连接设备:"+deviceID);
device = adb.waitForConnection(5000,deviceID);
}
if (device==null) return false;

return true;
}

/**
* 释放
*/
public void dispose() {
if (device != null) {
device.dispose();
device = null;
}
}

/**
* 摸拟touch方法
* @param x 坐标x
* @param y 坐标y
*
*/
public void touch(int x, int y,String type) {
if("DOWNANDUP".equals(type.toUpperCase())){
device.touch(x, y, TouchPressType.DOWN_AND_UP);
}else if("DOWN".equals(type.toUpperCase())){
device.touch(x, y, TouchPressType.DOWN);

}else if("UP".equals(type.toUpperCase())){
device.touch(x, y, TouchPressType.UP);
}
}
/**
* 截取屏幕
* @param filepath
*/
public void getScreenPic(String filepath){
IMonkeyImage newimage=device.takeSnapshot();
newimage.writeToFile(filepath, "png");
}
/**
* 这里的x,y,width,height为用来做比较的当前屏幕区域,index为用作比较的特征库索引<br />
* @see 特征库采集参见:
* @see #dimFeatures(int, int, int, int, int)
* @param x
* @param y
* @param width
* @param height
* @param index 要比较的特征库索引
* @param Expect 预期结果,意思指如果比较结果与预期结果相同则继续下面的脚本,否则挂起脚本
* @return 是否是预期结果
*/
public boolean isSamePic(int x,int y,int width,int height,int index,String Expect){
IMonkeyImage newimage=device.takeSnapshot().getSubImage(x, y, width, height);
index--;
if (index<0 || index>9)
return false;
if(featuresArr[index]==null)
return false;
/*newimage.writeToFile("d:\\newimage.png", "png");
featuresArr[index].writeToFile("d:\\feature.png", "png");*/
return newimage.sameAs(featuresArr[index], 0.90D)==Boolean.getBoolean(Expect);
}
/**
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式