Android无障碍服务执行用户手势的三类技术问题咨询
Hey there! Let's break down your questions about Performing Gestures in Android Accessibility Services, based on the official docs you're working with:
1. Do Performing Gestures operate above or below the touch-and-explore layer?
Performing Gestures work below the touch-and-explore layer.
Touch-and-explore (touch exploration mode) is a system accessibility feature that intercepts raw touch input and converts it into focus-based navigation—like moving focus between UI elements when a user swipes. Performing Gestures, however, send raw touch events directly to the target app's window, completely bypassing the touch-and-explore processing pipeline. This means they act at a lower, more fundamental level than the touch-and-explore layer.
2. Do Performing Gestures trigger AccessibilityEvents, and can this cause recursion?
Yes, Performing Gestures do trigger AccessibilityEvents—and recursion is a real risk.
When your service performs a gesture, the system treats it as if a real user made the input. That means corresponding AccessibilityEvents (like gesture detection start/end events, or view-specific events triggered by the gesture) will be dispatched to all accessibility services, including your own.
If your service listens for the same gesture it's performing (e.g., a right-swipe triggers your service to perform another right-swipe), the event from the performed swipe will re-trigger your listener, creating a recursive loop. To avoid this, you can:
- Temporarily disable your gesture listener while the service is executing a gesture
- Use a boolean flag to track when your service is actively performing a gesture, and skip processing events when this flag is set
3. How can I confirm a Performing Gesture was successfully executed?
There’s no direct, built-in way to confirm a gesture was "successful" (i.e., that the target app responded as intended), but you can use indirect methods to verify:
- Listen for expected AccessibilityEvents: If the gesture should trigger a specific UI change (like a screen transition or button click), wait for the corresponding event to confirm the gesture had the desired effect.
- Check UI state with
AccessibilityNodeInfo: After performing the gesture, query the UI to see if the expected state change occurred. For example, if you swiped to switch tabs, check if the active tab has updated. - Use timeouts: If you don’t see the expected event or state change within a reasonable window (e.g., 500ms to 1 second), assume the gesture may have failed and handle it accordingly.
- Leverage
OnGestureResultCallback(API 30+): Starting with Android 11, you can pass anOnGestureResultCallbackwhen callingdispatchGesture(). This callback will notify you if the system successfully received and queued the gesture (note: this only confirms the system accepted the gesture, not that the app processed it correctly).
内容的提问来源于stack exchange,提问作者Ashfaq

