失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Android 实现自动点击屏幕的方法

Android 实现自动点击屏幕的方法

时间:2023-09-29 01:56:08

相关推荐

Android  实现自动点击屏幕的方法

//新建一个实体类import android.app.Activity;import java.io.IOException;/*** Created by cxf on /9/29* 自动点击屏幕*/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() {@Overridepublic void run() {// 线程睡眠0.1stry {Thread.sleep(100);} 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() {@Overridepublic void run() {// 线程睡眠0.1stry {Thread.sleep(100);} 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 实现自动点击屏幕的方法》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。