Android:自动点击屏幕
2017-10-20 14:12
190 查看
在开发的过程中会遇到自动测试功能,比如如何自动点击按钮进行测试,当然可以使用按键精灵等工具进行测试,不过如何在程序进程中测试呢,下面就介绍下adb shell的操作,通过shell进行点击等操作。
1.模拟滑动
2.单击某点
3.长按某点
4.单击某个键
5.长按某个键
一、如下以点击为例,传入当期点所在屏幕位置或者比例
二、使用方法
1.模拟滑动
input swipe startX startY endX endY duration(ms)
2.单击某点
input tap x y
3.长按某点
input touchscreen swipe x y x y duration(ms)
4.单击某个键
input keyevent keyCode
5.长按某个键
input keyevent --longpress keyCode
一、如下以点击为例,传入当期点所在屏幕位置或者比例
package cn.test.autotouch; import android.app.Activity; import java.io.IOException; public class AutoTouch { public int width = 0; public int height = 0; /** * 传入在屏幕中的比例位置,坐标左上角为基准 * @param act 传入Activity对象 * @param ratioX 需要点击的x坐标在屏幕中的比例位置 * @param ratioY 需要点击的y坐标在屏幕中的比例位置 */ public void autoClickRatio(Activity act, final double ratioX, final double ratioY) { width = act.getWindowManager().getDefaultDisplay().getWidth(); height = act.getWindowManager().getDefaultDisplay().getHeight(); new Thread(new Runnable() { @Override public void run() { // 线程睡眠0.3s try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } // 生成点击坐标 int x = (int) (width * ratioX); int y = (int) (height * ratioY); // 利用ProcessBuilder执行shell命令 String[] order = { "input", "tap", "" + x, "" + y }; try { new ProcessBuilder(order).start(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } /** * 传入在屏幕中的坐标,坐标左上角为基准 * @param act 传入Activity对象 * @param x 需要点击的x坐标 * @param y 需要点击的x坐标 */ public void autoClickPos(Activity act, final double x, final double y) { width = act.getWindowManager().getDefaultDisplay().getWidth(); height = act.getWindowManager().getDefaultDisplay().getHeight(); new Thread(new Runnable() { @Override public void run() { // 线程睡眠0.3s try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } // 利用ProcessBuilder执行shell命令 String[] order = { "input", "tap", "" + x, "" + y }; try { new ProcessBuilder(order).start(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } }
二、使用方法
/***************************定义*****************************/ //声明一个Activity public static Activity staticActivity; //初始化AutoTouch对象 public static AutoTouch autoTouch = new AutoTouch(); //在onCreate中对staticActivity赋值 staticActivity = this; /***************************使用*****************************/ //传入所在比例 autoTouch.autoClickRatio(staticActivity, 0.4375, 0.537); //出入坐标 autoTouch.autoClickPos(staticActivity, 840, 580);
相关文章推荐
- 自动点击屏幕-调用android隐藏的功能
- Android 点击屏幕除TextView区域软键盘自动消失
- android App一段时间没有操作或没有点击屏幕后弹出广告,或者一段时间没有点击屏幕进行特定操作。
- Android 根据屏幕分辨率自动调整字体大小
- Android 处理屏幕点击
- android中关于点击屏幕,实现破碎的效果的实现
- Android 4.0设置Dialog点击屏幕不消失
- Android标题水平滚动条点击后自动移动
- Android中TextView自动识别url且实现点击跳转
- Android通过代码模拟物理、屏幕点击事件
- android:点击popupwindow以外区域 popupwindow自动消失
- android textview自动识别网址,点击长按选择文本
- Android 设置dialog 点击返回键及屏幕不消失
- Android 不同尺寸屏幕适配采用自动缩放完美解决
- Android 解决TableRow中TextView或Edittext超出屏幕,不能自动换行或换行问题
- Android开发之文件下载,状态时显示下载进度,点击自动安装
- [Android]搜索界面--自动弹出键盘,并点击键盘搜索进行搜索
- android 工程,点击图片,在屏幕显示
- Android真机调试自动点亮屏幕(基于CyanogenMod源码修改)
- Android模拟屏幕点击input tap替代解决方案