java.lang.Object | ||||
↳ | android.content.Context | |||
↳ | android.content.ContextWrapper | |||
↳ | android.app.Service | |||
↳ | android.accessibilityservice.AccessibilityService |
An accessibility service runs in the background and receives callbacks by the system
when AccessibilityEvent
s are fired. Such events denote some state transition
in the user interface, for example, the focus has changed, a button has been clicked,
etc. Such a service can optionally request the capability for querying the content
of the active window. Development of an accessibility service requires extending this
class and implementing its abstract methods.
The lifecycle of an accessibility service is managed exclusively by the system and
follows the established service life cycle. Additionally, starting or stopping an
accessibility service is triggered exclusively by an explicit user action through
enabling or disabling it in the device settings. After the system binds to a service it
calls onServiceConnected()
. This method can be
overriden by clients that want to perform post binding setup.
An accessibility is declared as any other service in an AndroidManifest.xml but it
must also specify that it handles the "android.accessibilityservice.AccessibilityService"
Intent
. Failure to declare this intent will cause the system to
ignore the accessibility service. Following is an example declaration:
<service android:name=".MyAccessibilityService"> <intent-filter> <action android:name="android.accessibilityservice.AccessibilityService" /> </intent-filter> . . . </service>
An accessibility service can be configured to receive specific types of accessibility events, listen only to specific packages, get events from each type only once in a given time frame, retrieve window content, specify a settings activity, etc.
There are two approaches for configuring an accessibility service:
meta-data
entry in the manifest when declaring
the service. A service declaration with a meta-data tag is presented below:
<service android:name=".MyAccessibilityService"> <intent-filter> <action android:name="android.accessibilityservice.AccessibilityService" /> </intent-filter> <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibilityservice" /> </service>
Note: This approach enables setting all properties.
For more details refer to SERVICE_META_DATA
and
<
.
accessibility-service
>
setServiceInfo(AccessibilityServiceInfo)
. Note
that this method can be called any time to dynamically change the service configuration.
Note: This approach enables setting only dynamically configurable properties:
eventTypes
,
feedbackType
,
flags
,
notificationTimeout
,
packageNames
For more details refer to AccessibilityServiceInfo
.
An service can specify in its declaration that it can retrieve the active window
content which is represented as a tree of AccessibilityNodeInfo
. Note that
declaring this capability requires that the service declares its configuration via
an XML resource referenced by SERVICE_META_DATA
.
For security purposes an accessibility service can retrieve only the content of the
currently active window. The currently active window is defined as the window from
which was fired the last event of the following types:
TYPE_WINDOW_STATE_CHANGED
,
TYPE_VIEW_HOVER_ENTER
,
TYPE_VIEW_HOVER_EXIT
,
In other words, the last window that was shown or the last window that the user has touched
during touch exploration.
The entry point for retrieving window content is through calling
AccessibilityEvent.getSource()
of the last received
event of the above types or a previous event from the same window
(see AccessibilityEvent.getWindowId()
). Invoking
this method will return an AccessibilityNodeInfo
that can be used to traverse the
window content which represented as a tree of such objects.
Note An accessibility service may have requested to be notified for a subset of the event types, thus be unaware that the active window has changed. Therefore accessibility service that would like to retrieve window content should:
getWindowId()
of the last received event and
compare this with the getWindowId()
before calling retrieval
methods on the latter.
AccessibilityNodeInfo
may fail since the
active window has changed and the service did not get the accessibility event yet. Note
that it is possible to have a retrieval method failing even adopting the strategy
specified in the previous bullet because the accessibility event dispatch is asynchronous
and crosses process boundaries.
For each feedback type only one accessibility service is notified. Services are notified in the order of registration. Hence, if two services are registered for the same feedback type in the same package the first one wins. It is possible however, to register a service as the default one for a given feedback type. In such a case this service is invoked if no other service was interested in the event. In other words, default services do not compete with other services and are notified last regardless of the registration order. This enables "generic" accessibility services that work reasonably well with most applications to coexist with "polished" ones that are targeted for specific applications.
Note: The event notification timeout is useful to avoid propagating events to the client too frequently since this is accomplished via an expensive interprocess call. One can think of the timeout as a criteria to determine when event generation has settled down.
TYPE_VIEW_CLICKED
TYPE_VIEW_LONG_CLICKED
TYPE_VIEW_FOCUSED
TYPE_VIEW_SELECTED
TYPE_VIEW_TEXT_CHANGED
TYPE_WINDOW_STATE_CHANGED
TYPE_NOTIFICATION_STATE_CHANGED
TYPE_TOUCH_EXPLORATION_GESTURE_START
TYPE_TOUCH_EXPLORATION_GESTURE_END
TYPE_VIEW_HOVER_ENTER
TYPE_VIEW_HOVER_EXIT
TYPE_VIEW_SCROLLED
TYPE_VIEW_TEXT_SELECTION_CHANGED
TYPE_WINDOW_CONTENT_CHANGED
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
String | SERVICE_INTERFACE | The Intent that must be declared as handled by the service. |
|||||||||
String | SERVICE_META_DATA | Name under which an AccessibilityService component publishes information about itself. |
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.app.Service
| |||||||||||
From class
android.content.Context
| |||||||||||
From interface
android.content.ComponentCallbacks2
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Callback for
AccessibilityEvent s. | |||||||||||
Implement to return the implementation of the internal accessibility
service interface.
| |||||||||||
Callback for interrupting the accessibility feedback.
| |||||||||||
Sets the
AccessibilityServiceInfo that describes this service. |
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
This method is a part of the
AccessibilityService lifecycle and is
called after the system has successfully bound to the service. |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.app.Service
| |||||||||||
From class
android.content.ContextWrapper
| |||||||||||
From class
android.content.Context
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
android.content.ComponentCallbacks
| |||||||||||
From interface
android.content.ComponentCallbacks2
|
The Intent
that must be declared as handled by the service.
Name under which an AccessibilityService component publishes information
about itself. This meta-data must reference an XML resource containing an
<
tag. This is a a sample XML file configuring an accessibility service:
accessibility-service
>
<accessibility-service android:accessibilityEventTypes="typeViewClicked|typeViewFocused" android:packageNames="foo.bar, foo.baz" android:accessibilityFeedbackType="feedbackSpoken" android:notificationTimeout="100" android:accessibilityFlags="flagDefault" android:settingsActivity="foo.bar.TestBackActivity" android:canRetrieveWindowContent="true" . . . />
Callback for AccessibilityEvent
s.
event | An event. |
---|
Implement to return the implementation of the internal accessibility service interface.
intent | The Intent that was used to bind to this service,
as given to Context.bindService . Note that any extras that were included with
the Intent at that point will not be seen here. |
---|
Callback for interrupting the accessibility feedback.
Sets the AccessibilityServiceInfo
that describes this service.
Note: You can call this method any time but the info will be picked up after the system has bound to this service and when this method is called thereafter.
info | The info. |
---|
This method is a part of the AccessibilityService
lifecycle and is
called after the system has successfully bound to the service. If is
convenient to use this method for setting the AccessibilityServiceInfo
.