如何在没有root的情况下可靠地模拟Android上的触摸事件(例如Automate和Tasker)? [英] How can I reliably simulate touch events on Android without root (like Automate and Tasker)?

查看:163
本文介绍了如何在没有root的情况下可靠地模拟Android上的触摸事件(例如Automate和Tasker)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我作为后台服务运行的应用之外的Java上可靠地模拟Android上的触摸事件(无需生根)?

How can I reliably simulate touch events on Android (without rooting) from Java outside my app which runs as a background service?

虽然之前已经问过这个问题,但大多数答案都使用亚行. (如如何在Android设备上模拟触摸事件? )

While this question has been asked before, most answers utilise ADB. (such as How to simulate touch events on Android device?)

https://github.com/chetbox/android-mouse-cursor 提供是使用Accessibility的一个很好的解决方案,但是它不是很可靠,因为并非所有视图都响应它,并且游戏在大多数时间都没有响应.

https://github.com/chetbox/android-mouse-cursor offers a good solution using Accessibility, but is not very reliable as not all views respond to it, and games do not respond at all most of the time.

private void click() {
  AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
  if (nodeInfo == null) return;

  AccessibilityNodeInfo nearestNodeToMouse = findSmallestNodeAtPoint(nodeInfo, cursorLayout.x, cursorLayout.y + 50);

  if (nearestNodeToMouse != null) {
    logNodeHierachy(nearestNodeToMouse, 0);
    nearestNodeToMouse.performAction(AccessibilityNodeInfo.ACTION_CLICK);
  }

  nodeInfo.recycle();
}

这是 https://github.com/chetbox/android-mouse使用的当前代码-光标.

Android版本为8.0,库存版Android

Android Version is 8.0, stock Android

是否有更好,更可靠的方法来模拟Java中的这些触摸事件?预先感谢!

Is there a better, more reliable way to simulate these touch events from Java? Thanks in advance!

推荐答案

如建议的那样,自牛轧糖(API 24)以来,模拟触摸事件的最佳方法是使用辅助功能和

As suggested, the best way to simulate touch events since Nougat (API 24) is by using an accessibility service and the AccessibilityService#dispatchGesture method.

这是我模拟一次点击事件的方法.

Here is how I did to simulate a single tap event.

// (x, y) in screen coordinates
private static GestureDescription createClick(float x, float y) {
    // for a single tap a duration of 1 ms is enough
    final int DURATION = 1;

    Path clickPath = new Path();
    clickPath.moveTo(x, y);
    GestureDescription.StrokeDescription clickStroke =
            new GestureDescription.StrokeDescription(clickPath, 0, DURATION);
    GestureDescription.Builder clickBuilder = new GestureDescription.Builder();
    clickBuilder.addStroke(clickStroke);
    return clickBuilder.build();
}

// callback invoked either when the gesture has been completed or cancelled
callback = new AccessibilityService.GestureResultCallback() {
    @Override
    public void onCompleted(GestureDescription gestureDescription) {
        super.onCompleted(gestureDescription);
        Log.d(TAG, "gesture completed");
    }

    @Override
    public void onCancelled(GestureDescription gestureDescription) {
        super.onCancelled(gestureDescription);
        Log.d(TAG, "gesture cancelled");
    }
};

// accessibilityService: contains a reference to an accessibility service
// callback: can be null if you don't care about gesture termination
boolean result = accessibilityService.dispatchGesture(createClick(x, y), callback, null);
Log.d(TAG, "Gesture dispatched? " + result);

要执行其他手势,您可能会发现

To perform other gestures, you might find useful the code used for testing the AccessibilityService#dispatchGesture implementation.

这篇关于如何在没有root的情况下可靠地模拟Android上的触摸事件(例如Automate和Tasker)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆