0
点赞
收藏
分享

微信扫一扫

Android通过adb命令实现模拟滑动


背景:

本文通过代码实现listview滑动功能,主要适于用自己开发的自动化测试


预备知识:

adb shell sendevent /dev/input/event0 3 0 110       //x坐标
adb shell sendevent /dev/input/event0 3 1 70         //y坐标
adb shell sendevent /dev/input/event0 1 330 1       //按下状态,准确的说是有压力值
adb shell sendevent /dev/input/event0 0 0 0           //必要的一行数据
adb shell sendevent /dev/input/event0 1 330 0       //抬起状态,无压力值
adb shell sendevent /dev/input/event0 0 0 0           //必要的一行,相当于终止一段完整数据的标致


模拟滑动轨迹
如下例是画出一条开始于(100,200),止于(108,200)的水平直线
adb shell sendevent /dev/input/event0 3 0 100 //start from point (100,200)
adb shell sendevent /dev/input/event0 3 1 200
   
adb shell sendevent /dev/input/event0 1 330 1 //touch
adb shell sendevent /dev/input/event0 0 0 0
   
adb shell sendevent /dev/input/event0 3 0 101 //step to point (101,200)
adb shell sendevent /dev/input/event0 0 0 0
……………………                                                  //must list each step, here just skip
adb shell sendevent /dev/input/event0 3 0 108 //end point(108,200)
adb shell sendevent /dev/input/event0 0 0 0
   
adb shell sendevent /dev/input/event0 1 330 0 //untouch
adb shell sendevent /dev/input/event0 0 0 0

核心代码:


注:下面代码只实现了从下往上拖拽的功能,至于向左、向右、向下拉等均可参照修改


/**
* @author chenzheng
* @since 2014-11-11
* @Description: 模拟滑动
* @throws
* @param x1
* @param y1
* @param x2
* @param y2
* void
*/
private static void dragView(int x1, int y1,int x2, int y2){
Log.e(tag, "drag " + x1 + "," + y1 + " to " + x2 + "," + y2);
List<String> pointList = new ArrayList<String>();
for(int j=y1-1; j>y2; j=j-50){
pointList.add("adb shell sendevent /dev/input/event0 3 1 " + j);
pointList.add("adb shell sendevent /dev/input/event0 0 0 0");
}
String[] cmds1 = {
"adb shell sendevent /dev/input/event0 3 0 " + x1,
"adb shell sendevent /dev/input/event0 3 1 " + y1,
"adb shell sendevent /dev/input/event0 1 330 1",
"adb shell sendevent /dev/input/event0 0 0 0"
};
String[] cmds2 = {
"adb shell sendevent /dev/input/event0 3 1 " + y2,
"adb shell sendevent /dev/input/event0 0 0 0",
"adb shell sendevent /dev/input/event0 1 330 0",
"adb shell sendevent /dev/input/event0 0 0 0"
};
try {
Process sh = Runtime.getRuntime().exec("su");
OutputStream os = sh.getOutputStream();
for (int i = 0; i < cmds1.length; i++) {
os.write((cmds1[i] + "\n").getBytes());
os.flush();
}
for (int i = 0; i < pointList.size(); i++) {
os.write((pointList.get(i) + "\n").getBytes());
os.flush();
}
for (int i = 0; i < cmds2.length; i++) {
os.write((cmds2[i] + "\n").getBytes());
os.flush();
}
os.write("exit\n".getBytes());
os.flush();
sh.waitFor();
os.close();
sh = null;
} catch (Exception e) {
e.printStackTrace();
}
}



​​​



举报

相关推荐

0 条评论