使用 AccessibilityService 在屏幕上滑动
Perform swipe on screen using 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);
确保您将服务配置为执行手势:
android:canPerformGestures="true"
正确设置路径:
p.moveTo(position.x, position.y);
p.lineTo(position.x + 300, position.y);
它只适用于 android 24 岁及以上。
我认为你有多个问题。你如何建立你的手势有点不对劲,你必须移动的像素数量比你想象的要大!我会根据屏幕尺寸而不是特定的像素数来计算。我认为典型的滑动手势大约是屏幕的一半,从一侧到另一侧,就在中间高度。
我设置了一个愚蠢的小 "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"
/>
编辑:
要支持向上或向下滑动,您只需更改路径参数。
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);
}
我想使用无障碍服务在屏幕上滑动。 我试过了,但这只执行一次触摸。 我知道这是可能的,因为在设备上启用我的服务时它说该服务可以执行滑动、触摸、捏合等操作
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);
确保您将服务配置为执行手势:
android:canPerformGestures="true"
正确设置路径:
p.moveTo(position.x, position.y);
p.lineTo(position.x + 300, position.y);
它只适用于 android 24 岁及以上。
我认为你有多个问题。你如何建立你的手势有点不对劲,你必须移动的像素数量比你想象的要大!我会根据屏幕尺寸而不是特定的像素数来计算。我认为典型的滑动手势大约是屏幕的一半,从一侧到另一侧,就在中间高度。
我设置了一个愚蠢的小 "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"
/>
编辑:
要支持向上或向下滑动,您只需更改路径参数。
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);
}