API 24 AccessibilityService.dispatchGesture() 方法如何工作?

How does the API 24 AccessibilityService.dispatchGesture() method work?

通过 API24,我们找到了一种向设备发送手势的方法,但是目前还没有可靠的文档或示例。我正在尝试让它工作,但目前手势每次都会触发 "onCancelled" 回调。

这是我调用该方法的代码:

@TargetApi(24)
private void pressLocation(Point position){
    GestureDescription.Builder builder = new GestureDescription.Builder();
    Path p = new Path();
    p.lineTo(position.x, position.y);
    p.lineTo(position.x+10, position.y+10);
    builder.addStroke(new GestureDescription.StrokeDescription(p, 10L, 200L));
    GestureDescription gesture = builder.build();
    boolean isDispatched = dispatchGesture(gesture, new GestureResultCallback() {
        @Override
        public void onCompleted(GestureDescription gestureDescription) {
            super.onCompleted(gestureDescription);
        }

        @Override
        public void onCancelled(GestureDescription gestureDescription) {
            super.onCancelled(gestureDescription);
        }
    }, null);

    Toast.makeText(FingerprintService.this, "Was it dispatched? " + isDispatched, Toast.LENGTH_SHORT).show();
}`

有没有人使用过这种新方法或知道如何让它发挥作用的示例?

您的路径只是 lineTos,没有指定起点。尝试将第一个更改为 moveTo.