In the first post, I talked about making apps accessible to all. In this second and final post I will explain how to create an accessibility service and what can be the intentions behind creating one.
How to create a custom accessibility service?
- Create a class that extends AccessibilityService and override onAccessibilityEvent(AccessibilityEvent event) and onInterrupt() methods.
- onAccessibilityEvent() – This method is called back by the system when it detects an AccessibilityEvent that matches the event filtering parameters specified by our accessibility service.
- onInterrupt() – This method is called when the system wants to interrupt the feedback our service is providing, usually in response to a user action such as moving focus to a different control.
- Make an entry of the above defined service in AndroidManifest file. Here, The action and permission are required by the system to identify it as an AccessibilityService.
- CustomAccessibilityService should have a configuration file which specifies the package names from which this service would like to receive events, the types of AccessibilityEvent that the service handles and information like whether the service can retrieve window content or not. Following are the descriptions of main entries of configuration file:
- android:settingsActivity – This is used to specify a settings activity for this service(if any).
- android:packageNames – This is used to specify package names of the apps this service should receive events from. If one would like their service to receive events from all apps then one can leave out this option.
- android:accessibilityEventTypes – This is used to specify the event types this service would listen to like typeViewClicked, typeViewLongClicked. For listening to all event types typeAllMask is used.
- android:accessibilityFeedbackType – The feedback types this service provides are specified using this attribute. Feedback types are like feedbackHaptic, feedbackSpoken.
- android:notificationTimeout – This is used to specify the minimal period in milliseconds between two accessibility events of the same type are sent to this service.
Other attributes are listed here. Add meta-data tag in Manifest declaration like this:
Add the following file in res/xml directory
For doing this programmatically, one has to override onServiceConnected method and use AccessibilityServiceInfo class to set the above mentioned parameters. However, not all configuration options are available using this method.
- Finally when the events defined in service configuration occur, the system fires Accessibility events which are caught by onAccessibilityEvent method overridden in CustomAccessibilityService where one can specify what action should be taken on respective events.
Below is a gist code where the service will speak out text from all text nodes present in the current window by using TextToSpeech engine on AccessibilityEvent typeWindowContentChanged. The demo application for this example can be found here.
Why custom accessibility service?
AccessibilityServices have been there in Android since Donut(API LEVEL 4) and its intention was to make OS and apps accessible for disabled people. But now organizations are using it for many other purposes. For example there are a few price comparison apps which use data from CustomAccessibilityService to figure out the product currently viewed by the user and provide alternative sites/apps for best prices of the respective product.
With the help of AccessibilityService we can retrieve window content of the device which is not available by other means, this opens a new horizon for application developers and the possibilities of utilizing this content are endless.
Explore it further and leave a comment for us about your use case of AccessibilityService.