我覺得你有很多問題。你如何構建你的手勢有點不合適,你必須移動的像素數量比你想象的要大!我會根據屏幕尺寸來計算,而不是特定數量的像素。我認爲典型的滑動手勢大約是屏幕的一半,從一側到另一側,正好在中間高度。
我設置了一個愚蠢的小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"
/>
所以我只能進行單點觸摸? –
好吧,我很可能是錯的,似乎API 24添加了一些東西...忘了我說的是什麼...... dispatchGesture返回什麼?對或錯? – pskink
它返回true –