【问题标题】:Android fake touch screen安卓假触摸屏
【发布时间】:2014-11-13 00:44:09
【问题描述】:

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

【问题讨论】:

  • 我确信一定有,因为您可能知道有一个名为“Monkey”的程序来测试您的应用程序...尝试谷歌搜索“猴子如何点击屏幕?”

标签: android touchscreen


【解决方案1】:

这个方法应该可以帮到你。

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

myButton.performClick();

【讨论】:

  • 是的......好吧..它适用于我的问题。最后,我希望后台服务中的所有这些都可以单击任何屏幕,而不仅仅是活动的屏幕。我想我将不得不为此使用猴子。
【解决方案2】:

以下代码会生成触摸事件,就像屏幕真的被触摸一样。

注意:仅限 root 设备

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2014-07-08
    • 2023-03-02
    • 1970-01-01
    相关资源
    最近更新 更多