使用AccessibilityService在屏幕上执行滑动 [英] Perform swipe on screen using AccessibilityService

查看:415
本文介绍了使用AccessibilityService在屏幕上执行滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to perform swipe on screen using accessibility service. i tried this but this only perform a single touch. i know it is possible because when enable my service on device it says this service can perform swipe,touch,pinch etc.

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);

推荐答案

我认为您有多个问题.如何构建手势有些不足,要使其滑动即可移动的像素数比您想象的要大!我会根据屏幕尺寸而不是特定数量的像素来计算.我认为典型的滑动手势大约是屏幕的一半,从一侧到另一侧,正好在中间高度方向上.

I think you have multiple problems. How you're building your gesture is a little off, and the number of pixels you have to move for it to be a swipe is bigger than you think! I would calculate this based on screen size, rather than a specific number of pixels. I think of a typical swipe gesture as about half the screen, originating from one side to the other, right in the middle height wise.

我设置了一个愚蠢的"onAccessibilityEvent"侦听器,该侦听器在Nexus 6上在主屏幕1和主屏幕2之间来回跳动.您必须先设置两个主屏幕才能看到它的运行状态.

I set up a silly little "onAccessibilityEvent" listener, that on my Nexus 6 bounces back and forth between home screen 1 and home screen two. You have to have two home screens set up obviously to see it in action.

@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文件

Also important is the accessibility configuration info, check out my config xml file

<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"
/>

要支持向上或向下滑动,只需更改路径参数即可.

To support swiping up or down you just need to change your path arguments.

final int height = displayMetrics.heightPixels;
final int top = height * .25;
final int mid = height * .5;
final int bottom = height * .75;
final int midX = displayMetrics.widthPixels / 2;

if(swipeUp) {
    path.moveTo(midX, bottom);
    path.lineTo(midX, top);
} else {
    path.moveTo(midX, top);
    path.lineTo(midX, bottom);
}

这篇关于使用AccessibilityService在屏幕上执行滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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