1. Introduction to Accessibility Service
Accessibility services are a feature of the Android framework designed to provide alternative navigation feedback to users on behalf of applications installed on Android devices. An accessibility service can communicate information about the application to the user, such as text-to-speech, or haptic feedback when the user’s finger hovers over an important area of the screen.
This section covers how to create an accessibility service, how to handle information received from applications, and how to provide feedback to the user.
2. Creating Your Own Accessibility Service
2.1 Inheriting from AccessibilityService
An accessibility service can be bundled with a standard application or created as a standalone Android project. In either case, the steps to create such a service are the same. In your project, create a class that extends AccessibilityService.
1 | import android.accessibilityservice.AccessibilityService; |
2.2 Declaring the Service in Manifest
Like any other service, you must declare it in the manifest file. Remember to specify that it handles the android.accessibilityservice.AccessibilityService intent, so that the service is called when an application triggers an AccessibilityEvent.
1 | <application> |
2.3 Configuring the Service
If you are creating a new project for this service and do not intend to have an application component, you can remove the Activity class (usually MainActivity.java) from your source files and the corresponding <activity> element from your manifest file.
Configuring Your Accessibility Service
You must provide configuration parameters for your accessibility service to tell the system how and when you want it to run. Which event types do you want to respond to? Is the service active for all applications or only specific package names? What kind of feedback does it use?
You have two ways to set these variables. The backward-compatible method is to set them in code using setServiceInfo(android.accessibilityservice.AccessibilityServiceInfo). To do this, override the onServiceConnected() method and configure your service there.
1 | import android.accessibilityservice.AccessibilityServiceInfo; |
Since Android 4.0, there is another way: verify the service using an XML file. If you define your service via XML, certain configurable options like canRetrieveWindowContent become available. The same configuration as above, defined in XML, would look like this:
1 | <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" |
If using the XML path, you must specify it in your manifest file by adding a <meta-data> tag to your service declaration pointing to the XML resource file. Assuming you stored your XML file at res/xml/serviceconfig.xml, the new tag would look like this:
1 | <service android:name=".MyAccessibilityService"> |
2.4 Responding to AccessibilityEvents
Now that your service is set up to run and listen for events, write some code so it knows what to do when an AccessibilityEvent actually arrives!
Start by overriding the onAccessibilityEvent(AccessibilityEvent) method. use getEventType() to determine the event type, and getContentDescription() to extract any label text associated with the event.
1 | import android.view.accessibility.AccessibilityNodeInfo; |
Querying the View Hierarchy for More Context
This step is optional but very useful. A feature introduced in Android 4.0 (API level 14) allows an AccessibilityService to query the view hierarchy to collect information about the UI component that generated the event, as well as its parent and children. To do this, make sure you have set the following in your XML configuration:
1 | android:canRetrieveWindowContent="true" |
If set, you can obtain an AccessibilityNodeInfo object via getSource(). If the window where the event originated is still the active window, this call returns an object; otherwise, it returns null. The following code demonstrates how to receive an event and perform the following steps:
- Immediately capture the parent of the view that triggered the event.
- In that view, look for a child view that is a label and a checkbox.
- If found, create a string to report to the user indicating whether the item is checked.
- If traversal of the view hierarchy returns null at any point, exit the method.
1 | // Alternative onAccessibilityEvent, that uses AccessibilityNodeInfo |
Now you have a complete, working accessibility service. You can also try configuring how it interacts with the user using Android’s text-to-speech engine or using the Vibrator for haptic feedback.
Finally, to use the configured service, you must go to “Settings -> Accessibility” and enable the corresponding service for it to respond to events.
About Me && Blog
Below is my personal intro and related links. I look forward to exchanging ideas with fellow professionals. “When three walk together, one can always be my teacher!”
- Blogger Intro
- Blog Content Navigation: A guide for my blog content.
- Curated Excellent Blog Articles - Android Performance Optimization Must-Knows
- Android Performance Optimization Knowledge Planet
One walks faster alone, but a group walks further together.
