如何在没有root的情况下可靠地模拟Android上的触摸事件(例如Automate和Tasker)?

The*_*heo 5 java android accessibilityservice android-8.0-oreo

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

尽管以前已经问过这个问题,但是大多数答案都是使用ADB的。(例如如何在Android设备上模拟触摸事件?

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

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();
}
Run Code Online (Sandbox Code Playgroud)

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

Android版本是8.0,库存Android

有没有更好,更可靠的方法来模拟Java中的这些触摸事件?提前致谢!

Ces*_*uri 8

如所建议的,自牛轧糖(API 24)以来,模拟触摸事件的最佳方法是使用可访问性服务和AccessibilityService#dispatchGesture方法。

这是我模拟单次点击事件的方式。

// (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);
Run Code Online (Sandbox Code Playgroud)

要执行其他手势,您可能会发现用于测试 AccessibilityService#dispatchGesture实现的代码很有

  • 有没有办法在 api 级别低于 24 的情况下实现触摸。我注意到您的应用程序“eva_facial_mouse”确实在 api 级别低于 24 的 Android 手机上执行点击操作。 (3认同)
  • @AkashKarnatak,在某种程度上这是可能的。EVA 使用 AccessibilityNodeInfo#performAction 方法来执行点击。这意味着您首先需要找到要点击的正确节点。这通常涉及从根开始搜索可访问性节点树。然而,根据我的经验,并非所有应用程序都提供这样的树,游戏是主要的罪犯。以下是我在 EVA 中实际实现的方法:https://github.com/cmauri/eva_facial_mouse/blob/a77b50e3104df3bb8ae3b11e23768b5171bd2462/eviacam/src/main/java/com/crea_si/eviacam/a11yservice/AccessibilityAction.java#L328 (3认同)
  • 这对我有用谢谢!应该注意的是,辅助服务 xml 需要具有“android:canPerformGestures=true”才能正常工作。 (2认同)