Perform Swipe On Screen Using Accessibilityservice
Solution 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.
@OverridepublicvoidonAccessibilityEvent(AccessibilityEvent event) {
switch (event.getEventType()) {
case AccessibilityEvent.TYPE_ANNOUNCEMENT:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
DisplayMetricsdisplayMetrics= getResources().getDisplayMetrics();
intmiddleYValue= displayMetrics.heightPixels / 2;
finalintleftSideOfScreen= displayMetrics.widthPixels / 4;
finalintrightSizeOfScreen= leftSideOfScreen * 3;
GestureDescription.BuildergestureBuilder=newGestureDescription.Builder();
Pathpath=newPath();
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(newGestureDescription.StrokeDescription(path, 100, 50));
dispatchGesture(gestureBuilder.build(), newGestureResultCallback() {
@OverridepublicvoidonCompleted(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"
/>
EDIT:
To support swiping up or down you just need to change your path arguments.
finalint height = displayMetrics.heightPixels;
finalint top = height * .25;
finalint mid = height * .5;
finalint bottom = height * .75;
finalint midX = displayMetrics.widthPixels / 2;
if(swipeUp) {
path.moveTo(midX, bottom);
path.lineTo(midX, top);
} else {
path.moveTo(midX, top);
path.lineTo(midX, bottom);
}
Solution 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.
Post a Comment for "Perform Swipe On Screen Using Accessibilityservice"