Android Guides | Samples

Android.AccessibilityServices.AccessibilityService Class

An accessibility service runs in the background and receives callbacks by the system when AccessibilityEvents are fired.

See Also: AccessibilityService

Syntax

[Android.Runtime.Register("android/accessibilityservice/AccessibilityService", DoNotGenerateAcw=true)]
public abstract class AccessibilityService : Service

Remarks

An accessibility service runs in the background and receives callbacks by the system when AccessibilityEvents 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.

Developer Guides

Lifecycle

Declaration

xml Example

 <service android:name=".MyAccessibilityService"
         android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
     <intent-filter>
         <action android:name="android.accessibilityservice.AccessibilityService" />
     </intent-filter>
     . . .
 </service>

Configuration

  • Providing a AccessibilityService.ServiceMetaData entry in the manifest when declaring the service. A service declaration with a meta-data tag is presented below:

    xml Example

     <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>
  • Calling AccessibilityService.SetServiceInfo(AccessibilityServiceInfo). Note that this method can be called any time to dynamically change the service configuration.

Retrieving window content

Notification strategy

Event types

Feedback types

For more information about creating AccessibilityServices, read the Accessibility developer guide.

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 AccessibilityService.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. Additionally an accessibility service must request the NoType:android/Manifest$permission;Href=../../../reference/android/Manifest.permission.html#BIND_ACCESSIBILITY_SERVICE permission to ensure that only the system can bind to it. Failure to declare this intent will cause the system to ignore the accessibility service. Following is an example declaration:

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:

Note: This approach enables setting all properties.

For more details refer to AccessibilityService.ServiceMetaData and <NoType:android/R$styleable;Href=../../../reference/android/R.styleable.html#AccessibilityService>.

Note: This approach enables setting only dynamically configurable properties: AccessibilityServiceInfo.EventTypes, AccessibilityServiceInfo.FeedbackType, AccessibilityServiceInfo.Flags, AccessibilityServiceInfo.NotificationTimeout, AccessibilityServiceInfo.PackageNames

For more details refer to AccessibilityServiceInfo.

A 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 AccessibilityService.ServiceMetaData.

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: EventTypes.WindowStateChanged, AccessibilityEventCompat.TypeViewHoverEnter, AccessibilityEventCompat.TypeViewHoverExit, 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 AccessibilityRecord.Source of the last received event of the above types or a previous event from the same window (see AccessibilityRecord.WindowId). 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:

  • Register for all event types with no notification timeout and keep track for the active window by calling AccessibilityRecord.WindowId of the last received event and compare this with the AccessibilityNodeInfo.WindowId before calling retrieval methods on the latter.
  • Prepare that a retrieval method on 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.

See Also

[Android Documentation]

Requirements

Namespace: Android.AccessibilityServices
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 4

The members of Android.AccessibilityServices.AccessibilityService are listed below.

See Also: Service

Public Constructors

Protected Constructors

A constructor used when creating managed representations of JNI objects; called by the runtime.

Public Fields

const
GestureSwipeDownAccessibilityGesture (2). The user has performed a swipe down gesture on the touch screen.
const
GestureSwipeDownAndLeftAccessibilityGesture (15). The user has performed an down and left gesture on the touch screen.
const
GestureSwipeDownAndRightAccessibilityGesture (16). The user has performed an down and right gesture on the touch screen.
const
GestureSwipeDownAndUpAccessibilityGesture (8). The user has performed a swipe down and up gesture on the touch screen.
const
GestureSwipeLeftAccessibilityGesture (3). The user has performed a swipe left gesture on the touch screen.
const
GestureSwipeLeftAndDownAccessibilityGesture (10). The user has performed a left and down gesture on the touch screen.
const
GestureSwipeLeftAndRightAccessibilityGesture (5). The user has performed a swipe left and right gesture on the touch screen.
const
GestureSwipeLeftAndUpAccessibilityGesture (9). The user has performed a left and up gesture on the touch screen.
const
GestureSwipeRightAccessibilityGesture (4). The user has performed a swipe right gesture on the touch screen.
const
GestureSwipeRightAndDownAccessibilityGesture (12). The user has performed a right and down gesture on the touch screen.
const
GestureSwipeRightAndLeftAccessibilityGesture (6). The user has performed a swipe right and left gesture on the touch screen.
const
GestureSwipeRightAndUpAccessibilityGesture (11). The user has performed a right and up gesture on the touch screen.
const
GestureSwipeUpAccessibilityGesture (1). The user has performed a swipe up gesture on the touch screen.
const
GestureSwipeUpAndDownAccessibilityGesture (7). The user has performed a swipe up and down gesture on the touch screen.
const
GestureSwipeUpAndLeftAccessibilityGesture (13). The user has performed an up and left gesture on the touch screen.
const
GestureSwipeUpAndRightAccessibilityGesture (14). The user has performed an up and right gesture on the touch screen.
const
GlobalActionBackGlobalAction (1). Action to go back.
const
GlobalActionHomeGlobalAction (2). Action to go home.
const
GlobalActionNotificationsGlobalAction (4). Action to open the notifications.
const
GlobalActionPowerDialogGlobalAction (6). Action to open the power long-press dialog.
const
GlobalActionQuickSettingsGlobalAction (5). Action to open the quick settings.
const
GlobalActionRecentsGlobalAction (3). Action to open the recent apps.
const
ServiceInterfaceString. The Intent that must be declared as handled by the service.
const
ServiceMetaDataString. Name under which an AccessibilityService component publishes information about itself.

Public Properties

[read-only]
RootInActiveWindowAccessibilityNodeInfo. Gets the root node in the currently active window if this service can retrieve window content.
[read-only]
ServiceInfoAccessibilityServiceInfo. Gets the an AccessibilityServiceInfo describing this AccessibilityService.
[read-only]
WindowsIList<Android.Views.Accessibility.AccessibilityWindowInfo>. Gets the windows on the screen.

Protected Properties

[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

FindFocus(NodeFocus) : AccessibilityNodeInfo
Find the view that has the specified focus type.
abstract
OnAccessibilityEvent(AccessibilityEvent)
Callback for AccessibilityEvents.
override
OnBind(Intent) : IBinder
Implement to return the implementation of the internal accessibility service interface.
abstract
OnInterrupt()
Callback for interrupting the accessibility feedback.
PerformGlobalAction(GlobalAction) : Boolean
Performs a global action.
SetServiceInfo(AccessibilityServiceInfo)
Sets the AccessibilityServiceInfo that describes this service.

Protected Methods

OnGesture(Int32) : Boolean
Called by the system when the user performs a specific gesture on the touch screen.
OnKeyEvent(KeyEvent) : Boolean
Callback that allows an accessibility service to observe the key events before they are passed to the rest of the system.
OnServiceConnected()
This method is a part of the AccessibilityService lifecycle and is called after the system has successfully bound to the service.