Android 安卓仿触摸屏

Android 安卓仿触摸屏,android,touchscreen,Android,Touchscreen,是否可能从代码中在屏幕上的特定点(例如(x,y))生成假触摸?我的活动上有一个按钮,我不想通过触摸屏点击它?有什么方法可以做到这一点吗?这种方法应该对你有所帮助 () 下面的代码生成触摸事件,就好像屏幕被真正触摸一样 警告:仅限根设备 public class Tap { private static final String SU = "su", TAG = Tap.class.getSimpleName(), COMMAND = "/system/bin/input tap

是否可能从代码中在屏幕上的特定点(例如(x,y))生成假触摸?我的活动上有一个按钮,我不想通过触摸屏点击它?有什么方法可以做到这一点吗?

这种方法应该对你有所帮助

()


下面的代码生成触摸事件,就好像屏幕被真正触摸一样

警告:仅限根设备

public class Tap {
private static final String SU = "su", TAG = Tap.class.getSimpleName(),
        COMMAND = "/system/bin/input tap %d %d ", ASCII = "ASCII";

public Tap() {

}

public void tap(int x1, int y1) {
    TapTask t = new TapTask(x1, y1);
    t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

private class TapTask extends AsyncTask<Void, Void, Void> {
    private int x1, y1;

    public TapTask(int x1, int y1) {
        this.x1 = x1;

        this.y1 = y1;

    }

    protected Void doInBackground(Void... args) {
        try {
            Process sh = Runtime.getRuntime().exec(SU, null, null);

            OutputStream os = sh.getOutputStream();

            os.write((String.format(COMMAND, x1,y1)).getBytes(ASCII));
            os.flush();
            os.close();
            sh.waitFor();

            Log.i(TAG,String.format("tap %d %d ",x1,y1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
}
公共类点击{
私有静态最终字符串SU=“SU”,TAG=Tap.class.getSimpleName(),
COMMAND=“/system/bin/input tap%d%d”,ASCII=“ASCII”;
公共水龙头(){
}
公共空分接头(内部x1、内部y1){
TapTask t=新的TapTask(x1,y1);
t、 executeOnExecutor(AsyncTask.THREAD\u POOL\u EXECUTOR);
}
私有类TapTask扩展了AsyncTask{
私人int x1,y1;
公共TapTask(int x1,int y1){
这是1.x1=x1;
这是1.y1=y1;
}
受保护的Void doInBackground(Void…args){
试一试{
Process sh=Runtime.getRuntime().exec(SU,null,null);
OutputStream os=sh.getOutputStream();
写((String.format(COMMAND,x1,y1)).getBytes(ASCII));
os.flush();
os.close();
sh.waitFor();
Log.i(标记,字符串格式(“点击%d%d”,x1,y1));
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
}
}

我相信一定会有,正如您可能知道的,有一个名为“Monkey”的程序来测试您的应用程序……试试谷歌搜索“Monkey在屏幕上点击的方式?”是的。。。好的。。这对我的问题有效。最后,我希望所有这些都能通过后台服务点击任何屏幕,而不仅仅是活动屏幕。我想我得用猴子来做那件事。
public class Tap {
private static final String SU = "su", TAG = Tap.class.getSimpleName(),
        COMMAND = "/system/bin/input tap %d %d ", ASCII = "ASCII";

public Tap() {

}

public void tap(int x1, int y1) {
    TapTask t = new TapTask(x1, y1);
    t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

private class TapTask extends AsyncTask<Void, Void, Void> {
    private int x1, y1;

    public TapTask(int x1, int y1) {
        this.x1 = x1;

        this.y1 = y1;

    }

    protected Void doInBackground(Void... args) {
        try {
            Process sh = Runtime.getRuntime().exec(SU, null, null);

            OutputStream os = sh.getOutputStream();

            os.write((String.format(COMMAND, x1,y1)).getBytes(ASCII));
            os.flush();
            os.close();
            sh.waitFor();

            Log.i(TAG,String.format("tap %d %d ",x1,y1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
}