public abstract class AccessibilityService extends Service
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.
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 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
android.Manifest.permission#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:
<service android:name=".MyAccessibilityService" android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"> <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:
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 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:
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
,
AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
,
AccessibilityEvent.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:
AccessibilityRecord.getWindowId()
of the last received event and
compare this with the AccessibilityNodeInfo.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.
AccessibilityEvent.TYPE_VIEW_CLICKED
AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
AccessibilityEvent.TYPE_VIEW_FOCUSED
AccessibilityEvent.TYPE_VIEW_SELECTED
AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED
AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START
AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END
AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
AccessibilityEvent.TYPE_VIEW_SCROLLED
AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED
Modifier and Type | Field and Description |
---|---|
static int |
GESTURE_SWIPE_DOWN
The user has performed a swipe down gesture on the touch screen.
|
static int |
GESTURE_SWIPE_DOWN_AND_LEFT
The user has performed an down and left gesture on the touch screen.
|
static int |
GESTURE_SWIPE_DOWN_AND_RIGHT
The user has performed an down and right gesture on the touch screen.
|
static int |
GESTURE_SWIPE_DOWN_AND_UP
The user has performed a swipe down and up gesture on the touch screen.
|
static int |
GESTURE_SWIPE_LEFT
The user has performed a swipe left gesture on the touch screen.
|
static int |
GESTURE_SWIPE_LEFT_AND_DOWN
The user has performed a left and down gesture on the touch screen.
|
static int |
GESTURE_SWIPE_LEFT_AND_RIGHT
The user has performed a swipe left and right gesture on the touch screen.
|
static int |
GESTURE_SWIPE_LEFT_AND_UP
The user has performed a left and up gesture on the touch screen.
|
static int |
GESTURE_SWIPE_RIGHT
The user has performed a swipe right gesture on the touch screen.
|
static int |
GESTURE_SWIPE_RIGHT_AND_DOWN
The user has performed a right and down gesture on the touch screen.
|
static int |
GESTURE_SWIPE_RIGHT_AND_LEFT
The user has performed a swipe right and left gesture on the touch screen.
|
static int |
GESTURE_SWIPE_RIGHT_AND_UP
The user has performed a right and up gesture on the touch screen.
|
static int |
GESTURE_SWIPE_UP
The user has performed a swipe up gesture on the touch screen.
|
static int |
GESTURE_SWIPE_UP_AND_DOWN
The user has performed a swipe up and down gesture on the touch screen.
|
static int |
GESTURE_SWIPE_UP_AND_LEFT
The user has performed an up and left gesture on the touch screen.
|
static int |
GESTURE_SWIPE_UP_AND_RIGHT
The user has performed an up and right gesture on the touch screen.
|
static int |
GLOBAL_ACTION_BACK
Action to go back.
|
static int |
GLOBAL_ACTION_HOME
Action to go home.
|
static int |
GLOBAL_ACTION_NOTIFICATIONS
Action to open the notifications.
|
static int |
GLOBAL_ACTION_QUICK_SETTINGS
Action to open the quick settings.
|
static int |
GLOBAL_ACTION_RECENTS
Action to open the recent apps.
|
static String |
SERVICE_INTERFACE
The
Intent that must be declared as handled by the service. |
static String |
SERVICE_META_DATA
Name under which an AccessibilityService component publishes information
about itself.
|
START_CONTINUATION_MASK, START_FLAG_REDELIVERY, START_FLAG_RETRY, START_NOT_STICKY, START_REDELIVER_INTENT, START_STICKY, START_STICKY_COMPATIBILITY, START_TASK_REMOVED_COMPLETE
ACCESSIBILITY_SERVICE, ACCOUNT_SERVICE, ACTIVITY_SERVICE, ALARM_SERVICE, APPWIDGET_SERVICE, AUDIO_SERVICE, BACKUP_SERVICE, BIND_ABOVE_CLIENT, BIND_ADJUST_WITH_ACTIVITY, BIND_ALLOW_OOM_MANAGEMENT, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_IMPORTANT, BIND_NOT_FOREGROUND, BIND_NOT_VISIBLE, BIND_VISIBLE, BIND_WAIVE_PRIORITY, BLUETOOTH_SERVICE, CLIPBOARD_SERVICE, CONNECTIVITY_SERVICE, CONTEXT_IGNORE_SECURITY, CONTEXT_INCLUDE_CODE, CONTEXT_RESTRICTED, COUNTRY_DETECTOR, DEVICE_POLICY_SERVICE, DISPLAY_SERVICE, DOWNLOAD_SERVICE, DROPBOX_SERVICE, INPUT_METHOD_SERVICE, INPUT_SERVICE, KEYGUARD_SERVICE, LAYOUT_INFLATER_SERVICE, LOCATION_SERVICE, MEDIA_ROUTER_SERVICE, MODE_APPEND, MODE_ENABLE_WRITE_AHEAD_LOGGING, MODE_MULTI_PROCESS, MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, NETWORK_POLICY_SERVICE, NETWORK_STATS_SERVICE, NETWORKMANAGEMENT_SERVICE, NFC_SERVICE, NOTIFICATION_SERVICE, NSD_SERVICE, POWER_SERVICE, SCHEDULING_POLICY_SERVICE, SEARCH_SERVICE, SENSOR_SERVICE, SERIAL_SERVICE, SIP_SERVICE, STATUS_BAR_SERVICE, STORAGE_SERVICE, TELEPHONY_SERVICE, TEXT_SERVICES_MANAGER_SERVICE, THROTTLE_SERVICE, UI_MODE_SERVICE, UPDATE_LOCK_SERVICE, USB_SERVICE, USER_SERVICE, VIBRATOR_SERVICE, WALLPAPER_SERVICE, WIFI_P2P_SERVICE, WIFI_SERVICE, WINDOW_SERVICE
TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_UI_HIDDEN
Constructor and Description |
---|
AccessibilityService() |
Modifier and Type | Method and Description |
---|---|
AccessibilityNodeInfo |
getRootInActiveWindow()
Gets the root node in the currently active window if this service
can retrieve window content.
|
AccessibilityServiceInfo |
getServiceInfo()
Gets the an
AccessibilityServiceInfo describing this
AccessibilityService . |
abstract void |
onAccessibilityEvent(AccessibilityEvent event)
Callback for
AccessibilityEvent s. |
IBinder |
onBind(Intent intent)
Implement to return the implementation of the internal accessibility
service interface.
|
protected boolean |
onGesture(int gestureId)
Called by the system when the user performs a specific gesture on the
touch screen.
|
abstract void |
onInterrupt()
Callback for interrupting the accessibility feedback.
|
protected void |
onServiceConnected()
This method is a part of the
AccessibilityService lifecycle and is
called after the system has successfully bound to the service. |
boolean |
performGlobalAction(int action)
Performs a global action.
|
void |
setServiceInfo(AccessibilityServiceInfo info)
Sets the
AccessibilityServiceInfo that describes this service. |
attach, dump, getApplication, onConfigurationChanged, onCreate, onDestroy, onLowMemory, onRebind, onStart, onStartCommand, onTaskRemoved, onTrimMemory, onUnbind, setForeground, startForeground, stopForeground, stopSelf, stopSelf, stopSelfResult
attachBaseContext, bindService, bindService, checkCallingOrSelfPermission, checkCallingOrSelfUriPermission, checkCallingPermission, checkCallingUriPermission, checkPermission, checkUriPermission, checkUriPermission, clearWallpaper, createConfigurationContext, createDisplayContext, createPackageContext, createPackageContextAsUser, databaseList, deleteDatabase, deleteFile, enforceCallingOrSelfPermission, enforceCallingOrSelfUriPermission, enforceCallingPermission, enforceCallingUriPermission, enforcePermission, enforceUriPermission, enforceUriPermission, fileList, getApplicationContext, getApplicationInfo, getAssets, getBaseContext, getCacheDir, getClassLoader, getCompatibilityInfo, getContentResolver, getDatabasePath, getDir, getExternalCacheDir, getExternalFilesDir, getFilesDir, getFileStreamPath, getMainLooper, getObbDir, getPackageCodePath, getPackageManager, getPackageName, getPackageResourcePath, getResources, getSharedPreferences, getSharedPrefsFile, getSystemService, getTheme, getThemeResId, getWallpaper, getWallpaperDesiredMinimumHeight, getWallpaperDesiredMinimumWidth, grantUriPermission, isRestricted, openFileInput, openFileOutput, openOrCreateDatabase, openOrCreateDatabase, peekWallpaper, registerReceiver, registerReceiver, registerReceiverAsUser, removeStickyBroadcast, removeStickyBroadcastAsUser, revokeUriPermission, sendBroadcast, sendBroadcast, sendBroadcastAsUser, sendBroadcastAsUser, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcastAsUser, sendStickyBroadcast, sendStickyBroadcastAsUser, sendStickyOrderedBroadcast, sendStickyOrderedBroadcastAsUser, setTheme, setWallpaper, setWallpaper, startActivities, startActivities, startActivitiesAsUser, startActivity, startActivity, startActivityAsUser, startActivityAsUser, startInstrumentation, startIntentSender, startIntentSender, startService, startServiceAsUser, stopService, stopServiceAsUser, unbindService, unregisterReceiver
getString, getString, getText, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, registerComponentCallbacks, unregisterComponentCallbacks
public static final int GESTURE_SWIPE_UP
public static final int GESTURE_SWIPE_DOWN
public static final int GESTURE_SWIPE_LEFT
public static final int GESTURE_SWIPE_RIGHT
public static final int GESTURE_SWIPE_LEFT_AND_RIGHT
public static final int GESTURE_SWIPE_RIGHT_AND_LEFT
public static final int GESTURE_SWIPE_UP_AND_DOWN
public static final int GESTURE_SWIPE_DOWN_AND_UP
public static final int GESTURE_SWIPE_LEFT_AND_UP
public static final int GESTURE_SWIPE_LEFT_AND_DOWN
public static final int GESTURE_SWIPE_RIGHT_AND_UP
public static final int GESTURE_SWIPE_RIGHT_AND_DOWN
public static final int GESTURE_SWIPE_UP_AND_LEFT
public static final int GESTURE_SWIPE_UP_AND_RIGHT
public static final int GESTURE_SWIPE_DOWN_AND_LEFT
public static final int GESTURE_SWIPE_DOWN_AND_RIGHT
public static final String SERVICE_INTERFACE
Intent
that must be declared as handled by the service.public static final String SERVICE_META_DATA
<accessibility-service
>
tag. This is a a sample XML file configuring an 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" . . . />
public static final int GLOBAL_ACTION_BACK
public static final int GLOBAL_ACTION_HOME
public static final int GLOBAL_ACTION_RECENTS
public static final int GLOBAL_ACTION_NOTIFICATIONS
public static final int GLOBAL_ACTION_QUICK_SETTINGS
public abstract void onAccessibilityEvent(AccessibilityEvent event)
AccessibilityEvent
s.event
- An event.public abstract void onInterrupt()
protected void onServiceConnected()
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
.protected boolean onGesture(int gestureId)
AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE
flag.gestureId
- The unique id of the performed gesture.GESTURE_SWIPE_UP
,
GESTURE_SWIPE_UP_AND_LEFT
,
GESTURE_SWIPE_UP_AND_DOWN
,
GESTURE_SWIPE_UP_AND_RIGHT
,
GESTURE_SWIPE_DOWN
,
GESTURE_SWIPE_DOWN_AND_LEFT
,
GESTURE_SWIPE_DOWN_AND_UP
,
GESTURE_SWIPE_DOWN_AND_RIGHT
,
GESTURE_SWIPE_LEFT
,
GESTURE_SWIPE_LEFT_AND_UP
,
GESTURE_SWIPE_LEFT_AND_RIGHT
,
GESTURE_SWIPE_LEFT_AND_DOWN
,
GESTURE_SWIPE_RIGHT
,
GESTURE_SWIPE_RIGHT_AND_UP
,
GESTURE_SWIPE_RIGHT_AND_LEFT
,
GESTURE_SWIPE_RIGHT_AND_DOWN
public AccessibilityNodeInfo getRootInActiveWindow()
public final boolean performGlobalAction(int action)
action
- The action to perform.GLOBAL_ACTION_BACK
,
GLOBAL_ACTION_HOME
,
GLOBAL_ACTION_NOTIFICATIONS
,
GLOBAL_ACTION_RECENTS
public final AccessibilityServiceInfo getServiceInfo()
AccessibilityServiceInfo
describing this
AccessibilityService
. This method is useful if one wants
to change some of the dynamically configurable properties at
runtime.AccessibilityNodeInfo
public final void setServiceInfo(AccessibilityServiceInfo info)
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.public final IBinder onBind(Intent intent)
onBind
in class Service
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.