Android通过adb命令实现模拟滑动背景:
android模拟点击本⽂通过代码实现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().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(((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();
}
}
转载请注明出处:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论