使用AccessibilityService在屏幕上执行滑动

时间:2017-06-07 18:33:58

标签: android accessibilityservice

我想使用辅助功能服务在屏幕上执行滑动。 我试过这个,但这只是一次触摸。 我知道这是可能的,因为当我在设备上启用我的服务时,它说这项服务可以执行滑动,触摸,捏等。

Point position=new Point(100,10);
GestureDescription.Builder builder = new GestureDescription.Builder();
Path p = new Path();
p.moveTo(position.x, position.y);
builder.addStroke(new GestureDescription.StrokeDescription(p, 100L, 50L));
GestureDescription gesture = builder.build();
boolean isDispatched =    dispatchGesture(gesture,gestureResultCallback,null);

2 个答案:

答案 0 :(得分:4)

我认为你有很多问题。你如何构建你的手势有点过时,你需要移动的像素数量比你想象的要大!我会根据屏幕尺寸而不是特定数量的像素来计算。我认为典型的滑动手势大约是屏幕的一半,从一侧到另一侧,正好在中间高度方向。

我设置了一个愚蠢的小“onAccessibilityEvent”监听器,我的Nexus 6在主屏幕1和主屏幕2之间来回跳转。您必须设置两个主屏幕才能看到它的运行情况。

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {

    switch (event.getEventType()) {
        case AccessibilityEvent.TYPE_ANNOUNCEMENT:
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {

                DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

                int middleYValue = displayMetrics.heightPixels / 2;
                final int leftSideOfScreen = displayMetrics.widthPixels / 4;
                final int rightSizeOfScreen = leftSideOfScreen * 3;
                GestureDescription.Builder gestureBuilder = new GestureDescription.Builder();
                Path path = new Path();

                if (event.getText() != null && event.getText().toString().contains("1")) {
                    //Swipe left
                    path.moveTo(rightSizeOfScreen, middleYValue);
                    path.lineTo(leftSideOfScreen, middleYValue);
                } else {
                    //Swipe right
                    path.moveTo(leftSideOfScreen, middleYValue);
                    path.lineTo(rightSizeOfScreen, middleYValue);
                }

                gestureBuilder.addStroke(new GestureDescription.StrokeDescription(path, 100, 50));
                dispatchGesture(gestureBuilder.build(), new GestureResultCallback() {
                    @Override
                    public void onCompleted(GestureDescription gestureDescription) {
                        Log.w("Gesture Completed");
                        super.onCompleted(gestureDescription);
                    }
                }, null);
            }

        default: {
            break;
        }
    }
}

同样重要的是辅助功能配置信息,请查看我的配置xml文件

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/accessibility_service_description"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagReportViewIds"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:settingsActivity="com.moba11y.basicaccessibilityservice.SettingsActivity"
android:canPerformGestures="true"
/>

答案 1 :(得分:0)

确保您已将服务配置为执行手势:

p.moveTo(position.x, position.y);
p.lineTo(position.x + 300, position.y); 

正确设置路径:

ROW_NUMBER() OVER(PARTITION BY tableName ORDER BY batchNum DESC) [rownum]

它仅适用于Android 24及更高版本。