开发者

Android fake touch screen

Is it possible to ge开发者_Go百科nerate a fake touch at a particular point (say (x,y)) on the screen from the code? There is a button on my activity and I dont want to click it through touch screen? Is there any way to achieve this?


This method should help you.

http://developer.android.com/reference/android/view/View.html#performClick()

myButton.performClick();


The following code generates touch event as if the screen is really touched.

CAUTION: rooted devices only

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;
    }
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜