问题
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);
回答1:
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.
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;
}
}
}
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"
/>
回答2:
Make sure you configured your service to perform gestures:
android:canPerformGestures="true"
Setup your path correctly:
p.moveTo(position.x, position.y);
p.lineTo(position.x + 300, position.y);
It only works on android 24 and above.
来源:https://stackoverflow.com/questions/44420320/perform-swipe-on-screen-using-accessibilityservice