问题描述
如何在我作为后台服务运行的应用之外的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();
}
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)以来,模拟触摸事件的最佳方法是使用辅助功能和 AccessibilityService#dispatchGesture 方法.
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);
要执行其他手势,您可能会发现用于测试的代码 AccessibilityService#dispatchGesture实现.
To perform other gestures, you might find useful the code used for testing the AccessibilityService#dispatchGesture implementation.
这篇关于如何在没有root的情况下可靠地模拟Android上的触摸事件(例如Automate和Tasker)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!