1 page.title=Building Accessibility Services 2 parent.title=Accessibility 3 parent.link=index.html 4 @jd:body 5 6 <div id="qv-wrapper"> 7 <div id="qv"> 8 9 <h2>Topics</h2> 10 <ol> 11 <li><a href="#manifest">Manifest Declarations and Permissions</a> 12 <ol> 13 <li><a href="#service-declaration">Accessibility service declaration</a></li> 14 <li><a href="#service-config">Accessibility service configuration</a></li> 15 </ol> 16 </li> 17 <li><a href="#register">Registering for Accessibility Events</a></li> 18 <li><a href="#methods">AccessibilityService Methods</a></li> 19 <li><a href="#event-details">Getting Event Details</a></li> 20 <li><a href="#act-for-users">Taking Action for Users</a> 21 <ol> 22 <li><a href="#detect-gestures">Listening for gestures</a></li> 23 <li><a href="#using-actions">Using accessibility actions</a></li> 24 <li><a href="#focus-types">Using focus types</a></li> 25 </ol> 26 </li> 27 <li><a href="#examples">Example Code</a></li> 28 </ol> 29 30 <h2>Key classes</h2> 31 <ol> 32 <li>{@link android.accessibilityservice.AccessibilityService}</li> 33 <li>{@link android.accessibilityservice.AccessibilityServiceInfo}</li> 34 <li>{@link android.view.accessibility.AccessibilityEvent}</li> 35 <li>{@link android.view.accessibility.AccessibilityRecord}</li> 36 <li>{@link android.view.accessibility.AccessibilityNodeInfo}</li> 37 </ol> 38 39 <h2>See also</h2> 40 <ol> 41 <li><a href="{@docRoot}training/accessibility/index.html">Training: Implementing Accessibility</a></li> 42 </ol> 43 44 </div> 45 </div> 46 47 <p>An accessibility service is an application that provides user interface enhancements to 48 assist users with disabilities, or who may temporarily be unable to fully interact with a device. 49 For example, users who are driving, taking care of a young child or attending a very loud party 50 might need additional or alternative interface feedback.</p> 51 52 <p>Android provides standard accessibility services, including TalkBack, and developers can 53 create and distribute their own services. This document explains the basics of building an 54 accessibility service.</p> 55 56 <p>The ability for you to build and deploy accessibility services was introduced with Android 1.6 57 (API Level 4) and received significant improvements with Android 4.0 (API Level 14). The Android 58 <a href="{@docRoot}tools/support-library/index.html">Support Library</a> was also updated with 59 the release of Android 4.0 to provide support for these enhanced accessibility features back to 60 Android 1.6. Developers aiming for widely compatible accessibility services are encouraged to use 61 the Support Library and develop for the more advanced accessibility features introduced in 62 Android 4.0.</p> 63 64 65 <h2 id="manifest">Manifest Declarations and Permissions</h2> 66 67 <p>Applications that provide accessibility services must include specific declarations in their 68 application manifests to be treated as an accessibility service by the Android system. This 69 section explains the required and optional settings for accessibility services.</p> 70 71 72 <h3 id="service-declaration">Accessibility service declaration</h3> 73 74 <p>In order to be treated as an accessibility service, you must include a 75 {@code service} element (rather than the {@code activity} element) within the {@code application} 76 element in your manifest. In addition, within the {@code service} element, you must also include an 77 accessibility service intent filter. For compatiblity with Android 4.1 and higher, the manifest 78 must also request the {@link android.Manifest.permission#BIND_ACCESSIBILITY_SERVICE} permission 79 as shown in the following sample:</p> 80 81 <pre> 82 <manifest> 83 ... 84 <uses-permission ... /> 85 ... 86 <application> 87 ... 88 <service android:name=".MyAccessibilityService" 89 android:label="@string/accessibility_service_label" 90 android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"> 91 <intent-filter> 92 <action android:name="android.accessibilityservice.AccessibilityService" /> 93 </intent-filter> 94 </service> 95 <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" /> 96 </application> 97 </manifest> 98 </pre> 99 100 <p>These declarations are required for all accessibility services deployed on Android 1.6 (API Level 101 4) or higher.</p> 102 103 104 <h3 id="service-config">Accessibility service configuration</h3> 105 106 <p>Accessibility services must also provide a configuration which specifies the types of 107 accessibility events that the service handles and additional information about the service. The 108 configuration of an accessibility service is contained in the {@link 109 android.accessibilityservice.AccessibilityServiceInfo} class. Your service can build and set a 110 configuration using an instance of this class and {@link 111 android.accessibilityservice.AccessibilityService#setServiceInfo setServiceInfo()} at runtime. 112 However, not all configuration options are available using this method.</p> 113 114 <p>Beginning with Android 4.0, you can include a {@code <meta-data>} element in your manifest 115 with a reference to a configuration file, which allows you to set the full range of options for 116 your accessibility service, as shown in the following example:</p> 117 118 <pre> 119 <service android:name=".MyAccessibilityService"> 120 ... 121 <meta-data 122 android:name="android.accessibilityservice" 123 android:resource="@xml/accessibility_service_config" /> 124 </service> 125 </pre> 126 127 <p>This meta-data element refers to an XML file that you create in your applications resource 128 directory ({@code <project_dir>/res/xml/accessibility_service_config.xml}). The following code 129 shows example contents for the service configuration file:</p> 130 131 <pre> 132 <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" 133 android:description="@string/accessibility_service_description" 134 android:packageNames="com.example.android.apis" 135 android:accessibilityEventTypes="typeAllMask" 136 android:accessibilityFlags="flagDefault" 137 android:accessibilityFeedbackType="feedbackSpoken" 138 android:notificationTimeout="100" 139 android:canRetrieveWindowContent="true" 140 android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity" 141 /> 142 </pre> 143 144 <p>For more information about the XML attributes which can be used in the accessibility service 145 configuration file, follow these links to the reference documentation:</p> 146 147 <ul> 148 <li><a href="{@docRoot}reference/android/R.styleable.html#AccessibilityService_description">{@code android:description}</a></li> 149 <li><a href="{@docRoot}reference/android/R.styleable.html#AccessibilityService_packageNames">{@code android:packageNames}</a></li> 150 <li><a href="{@docRoot}reference/android/R.styleable.html#AccessibilityService_accessibilityEventTypes">{@code android:accessibilityEventTypes}</a></li> 151 <li><a href="{@docRoot}reference/android/R.styleable.html#AccessibilityService_accessibilityFlags">{@code android:accessibilityFlags}</a></li> 152 <li><a href="{@docRoot}reference/android/R.styleable.html#AccessibilityService_accessibilityFeedbackType">{@code android:accessibilityFeedbackType}</a></li> 153 <li><a href="{@docRoot}reference/android/R.styleable.html#AccessibilityService_notificationTimeout">{@code android:notificationTimeout}</a></li> 154 <li><a href="{@docRoot}reference/android/R.styleable.html#AccessibilityService_canRetrieveWindowContent">{@code android:canRetrieveWindowContent}</a></li> 155 <li><a href="{@docRoot}reference/android/R.styleable.html#AccessibilityService_settingsActivity">{@code android:settingsActivity}</a></li> 156 </ul> 157 158 <p>For more information about which configuration settings can be dynamically set at runtime, see 159 the {@link android.accessibilityservice.AccessibilityServiceInfo} reference documentation.</p> 160 161 162 <h2 id="register">Registering for Accessibility Events</h2> 163 164 <p>One of the most important functions of the accessibility service configuration parameters is to 165 allow you to specify what types of accessibility events your service can handle. Being able to 166 specify this information enables accessibility services to cooperate with each other, and allows you 167 as a developer the flexibility to handle only specific events types from specific applications. The 168 event filtering can include the following criteria:</p> 169 170 <ul> 171 <li><strong>Package Names</strong> - Specify the package names of applications whose accessibility 172 events you want your service to handle. If this parameter is omitted, your accessibility service is 173 considered available to service accessibility events for any application. This parameter can be set 174 in the accessibility service configuration files with the {@code android:packageNames} attribute as 175 a comma-separated list, or set using the {@link 176 android.accessibilityservice.AccessibilityServiceInfo#packageNames 177 AccessibilityServiceInfo.packageNames} member.</li> 178 <li><strong>Event Types</strong> - Specify the types of accessibility events you want your service 179 to handle. This parameter can be set in the accessibility service configuration files with the 180 {@code android:accessibilityEventTypes} attribute as a list separated by the {@code |} character 181 (for example {@code accessibilityEventTypes="typeViewClicked|typeViewFocused"}), or set using the 182 {@link android.accessibilityservice.AccessibilityServiceInfo#eventTypes 183 AccessibilityServiceInfo.eventTypes} member. </li> 184 </ul> 185 186 <p>When setting up your accessibility service, carefully consider what events your service is able 187 to handle and only register for those events. Since users can activate more than one accessibility 188 services at a time, your service must not consume events that it is not able to handle. Remember 189 that other services may handle those events in order to improve a user's experience.</p> 190 191 <p class="note"><strong>Note:</strong> The Android framework dispatches accessibility events to 192 more than one accessibility service if the services provide different 193 <a href="{@docRoot}reference/android/R.styleable.html#AccessibilityService_accessibilityFeedbackType"> 194 feedback types</a>. However, if two or more services provide the same feedback type, then only the 195 first registered service receives the event.</p> 196 197 198 <h2 id="methods">AccessibilityService Methods</h2> 199 200 <p>An accessibility service must extend the {@link 201 android.accessibilityservice.AccessibilityService} class and override the following methods from 202 that class. These methods are presented in the order in which they are called by the Android system, 203 from when the service is started 204 ({@link android.accessibilityservice.AccessibilityService#onServiceConnected onServiceConnected()}), 205 while it is running ({@link android.accessibilityservice.AccessibilityService#onAccessibilityEvent 206 onAccessibilityEvent()}, 207 {@link android.accessibilityservice.AccessibilityService#onInterrupt onInterrupt()}) to when it is 208 shut down ({@link android.accessibilityservice.AccessibilityService#onUnbind onUnbind()}).</p> 209 210 <ul> 211 <li>{@link android.accessibilityservice.AccessibilityService#onServiceConnected 212 onServiceConnected()} - (optional) This system calls this method when it successfully connects to 213 your accessibility service. Use this method to do any one-time setup steps for your service, 214 including connecting to user feedback system services, such as the audio manager or device vibrator. 215 If you want to set the configuration of your service at runtime or make one-time adjustments, this 216 is a convenient location from which to call {@link 217 android.accessibilityservice.AccessibilityService#setServiceInfo setServiceInfo()}.</li> 218 219 <li>{@link android.accessibilityservice.AccessibilityService#onAccessibilityEvent 220 onAccessibilityEvent()} - (required) This method is called back by the system when it detects an 221 {@link android.view.accessibility.AccessibilityEvent} that matches the event filtering parameters 222 specified by your accessibility service. For example, when the user clicks a button or focuses on a 223 user interface control in an application for which your accessibility service is providing feedback. 224 When this happens, the system calls this method, passing the associated {@link 225 android.view.accessibility.AccessibilityEvent}, which the service can then interpret and use to 226 provide feedback to the user. This method may be called many times over the lifecycle of your 227 service.</li> 228 229 <li>{@link android.accessibilityservice.AccessibilityService#onInterrupt onInterrupt()} - 230 (required) This method is called when the system wants to interrupt the feedback your service is 231 providing, usually in response to a user action such as moving focus to a different control. This 232 method may be called many times over the lifecycle of your service.</li> 233 234 <li>{@link android.accessibilityservice.AccessibilityService#onUnbind onUnbind()} - (optional) 235 This method is called when the system is about to shutdown the accessibility service. Use this 236 method to do any one-time shutdown procedures, including de-allocating user feedback system 237 services, such as the audio manager or device vibrator.</li> 238 </ul> 239 240 <p>These callback methods provide the basic structure for your accessibility service. It is up to 241 you to decide on how to process data provided by the Android system in the form of {@link 242 android.view.accessibility.AccessibilityEvent} objects and provide feedback to the user. For more 243 information about getting information from an accessibility event, see the 244 <a href="{@docRoot}training/accessibility/service.html">Implementing Accessibility</a> training.</p> 245 246 247 <h2 id="event-details">Getting Event Details</h2> 248 249 <p>The Android system provides information to accessibility services about the user interface 250 interaction through {@link android.view.accessibility.AccessibilityEvent} objects. Prior to Android 251 4.0, the information available in an accessibility event, while providing a significant amount of 252 detail about a user interface control selected by the user, offered limited contextual 253 information. In many cases, this missing context information might be critical to understanding the 254 meaning of the selected control.</p> 255 256 <p>An example of an interface where context is critical is a calendar or day planner. If the 257 user selects a 4:00 PM time slot in a Monday to Friday day list and the accessibility service 258 announces 4 PM, but does not announce the weekday name, the day of the month, or the month name, 259 the resulting feedback is confusing. In this case, the context of a user interface control is 260 critical to a user who wants to schedule a meeting.</p> 261 262 <p>Android 4.0 significantly extends the amount of information that an accessibility service can 263 obtain about an user interface interaction by composing accessibility events based on the view 264 hierarchy. A view hierarchy is the set of user interface components that contain the component (its 265 parents) and the user interface elements that may be contained by that component (its children). In 266 this way, the Android system can provide much richer detail about accessibility events, allowing 267 accessibility services to provide more useful feedback to users.</p> 268 269 <p>An accessibility service gets information about an user interface event through an {@link 270 android.view.accessibility.AccessibilityEvent} passed by the system to the services 271 {@link android.accessibilityservice.AccessibilityService#onAccessibilityEvent 272 onAccessibilityEvent()} callback method. This object provides details about the event, including the 273 type of object being acted upon, its descriptive text and other details. Starting in Android 4.0 274 (and supported in previous releases through the {@link 275 android.support.v4.view.accessibility.AccessibilityEventCompat} object in the Support Library), you 276 can obtain additional information about the event using these calls:</p> 277 278 <ul> 279 <li>{@link android.view.accessibility.AccessibilityEvent#getRecordCount 280 AccessibilityEvent.getRecordCount()} and {@link 281 android.view.accessibility.AccessibilityEvent#getRecord getRecord(int)} - These methods allow you to 282 retrieve the set of {@link android.view.accessibility.AccessibilityRecord} objects which contributed 283 to the {@link android.view.accessibility.AccessibilityEvent} passed to you by the system. This level 284 of detail provides more context for the event that triggered your accessibility service.</li> 285 286 <li>{@link android.view.accessibility.AccessibilityEvent#getSource 287 AccessibilityEvent.getSource()} - This method returns an {@link 288 android.view.accessibility.AccessibilityNodeInfo} object. This object allows you to request view 289 layout hierarchy (parents and children) of the component that originated the accessibility event. 290 This feature allows an accessibility service to investigate the full context of an event, including 291 the content and state of any enclosing views or child views. 292 293 <p class="caution"><strong>Important:</strong> The ability to investigate the view 294 hierarchy from an {@link android.view.accessibility.AccessibilityEvent} potentially exposes private 295 user information to your accessibility service. For this reason, your service must request this 296 level of access through the accessibility <a href="#service-config">service configuration XML</a> 297 file, by including the {@code canRetrieveWindowContent} attribute and setting it to {@code true}. If 298 you do not include this setting in your service configuration xml file, calls to {@link 299 android.view.accessibility.AccessibilityEvent#getSource getSource()} fail.</p> 300 301 <p class="note"><strong>Note:</strong> In Android 4.1 (API Level 16) and higher, the 302 {@link android.view.accessibility.AccessibilityEvent#getSource getSource()} method, 303 as well as {@link android.view.accessibility.AccessibilityNodeInfo#getChild 304 AccessibilityNodeInfo.getChild()} and 305 {@link android.view.accessibility.AccessibilityNodeInfo#getParent getParent()}, return only 306 view objects that are considered important for accessibility (views that draw content or respond to 307 user actions). If your service requires all views, it can request them by setting the 308 {@link android.accessibilityservice.AccessibilityServiceInfo#flags flags} member of the service's 309 {@link android.accessibilityservice.AccessibilityServiceInfo} instance to 310 {@link android.accessibilityservice.AccessibilityServiceInfo#FLAG_INCLUDE_NOT_IMPORTANT_VIEWS}.</p> 311 </li> 312 </ul> 313 314 315 <h2 id="act-for-users">Taking Action for Users</h2> 316 317 <p>Starting with Android 4.0 (API Level 14), accessibility services can act on behalf 318 of users, including changing the input focus and selecting (activating) user interface elements. 319 In Android 4.1 (API Level 16) the range of actions has been expanded to include scrolling lists 320 and interacting with text fields. Accessibility services can 321 also take global actions, such as navigating to the Home screen, pressing the Back button, opening 322 the notifications screen and recent applications list. Android 4.1 also includes a new type of 323 focus, <em>Accessibilty Focus</em>, which makes all visible elements selectable by an 324 accessibility service.</p> 325 326 <p>These new capabilities make it possible for developers of accessibility services to create 327 alternative navigation modes such as 328 <a href="{@docRoot}tools/testing/testing_accessibility.html#test-gestures">gesture navigation</a>, 329 and give users with disabilities improved control of their Android devices.</p> 330 331 332 <h3 id="detect-gestures">Listening for gestures</h3> 333 334 <p>Accessibility services can listen for specific gestures and respond by taking action on behalf 335 of a user. This feature, added in Android 4.1 (API Level 16), and requires that your 336 accessibility service request activation of the Explore by Touch feature. Your service can 337 request this activation by setting the 338 {@link android.accessibilityservice.AccessibilityServiceInfo#flags flags} member of the services 339 {@link android.accessibilityservice.AccessibilityServiceInfo} instance to 340 {@link android.accessibilityservice.AccessibilityServiceInfo#FLAG_REQUEST_TOUCH_EXPLORATION_MODE}, 341 as shown in the following example. 342 </p> 343 344 <pre> 345 public class MyAccessibilityService extends AccessibilityService { 346 @Override 347 public void onCreate() { 348 getServiceInfo().flags = AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE; 349 } 350 ... 351 } 352 </pre> 353 354 <p>Once your service has requested activation of Explore by Touch, the user must allow the 355 feature to be turned on, if it is not already active. When this feature is active, your service 356 receives notification of accessibility gestures through your service's 357 {@link android.accessibilityservice.AccessibilityService#onGesture onGesture()} callback method 358 and can respond by taking actions for the user.</p> 359 360 361 <h3 id="using-actions">Using accessibility actions</h3> 362 363 <p>Accessibility services can take action on behalf of users to make interacting with applications 364 simpler and more productive. The ability of accessibility services to perform actions was added 365 in Android 4.0 (API Level 14) and significantly expanded with Android 4.1 (API Level 16).</p> 366 367 <p>In order to take actions on behalf of users, your accessibility service must 368 <a href="#register">register</a> to receive events from a few or many applications and request 369 permission to view the content of applications by setting the 370 <a href="{@docRoot}reference/android/R.styleable.html#AccessibilityService_canRetrieveWindowContent"> 371 {@code android:canRetrieveWindowContent}</a> to {@code true} in the 372 <a href="#service-config">service configuration file</a>. When events are received by your 373 service, it can then retrieve the 374 {@link android.view.accessibility.AccessibilityNodeInfo} object from the event using 375 {@link android.view.accessibility.AccessibilityEvent#getSource getSource()}. 376 With the {@link android.view.accessibility.AccessibilityNodeInfo} object, your service can then 377 explore the view hierarchy to determine what action to take and then act for the user using 378 {@link android.view.accessibility.AccessibilityNodeInfo#performAction performAction()}.</p> 379 380 <pre> 381 public class MyAccessibilityService extends AccessibilityService { 382 383 @Override 384 public void onAccessibilityEvent(AccessibilityEvent event) { 385 // get the source node of the event 386 AccessibilityNodeInfo nodeInfo = event.getSource(); 387 388 // Use the event and node information to determine 389 // what action to take 390 391 // take action on behalf of the user 392 nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); 393 394 // recycle the nodeInfo object 395 nodeInfo.recycle(); 396 } 397 ... 398 } 399 </pre> 400 401 <p>The {@link android.view.accessibility.AccessibilityNodeInfo#performAction performAction()} method 402 allows your service to take action within an application. If your service needs to perform a 403 global action such as navigating to the Home screen, pressing the Back button, opening the 404 notifications screen or recent applications list, then use the 405 {@link android.accessibilityservice.AccessibilityService#performGlobalAction performGlobalAction()} 406 method.</p> 407 408 409 <h3 id="focus-types">Using focus types</h3> 410 411 <p>Android 4.1 (API Level 16) introduces a new type of user interface focus called <em>Accessibility 412 Focus</em>. This type of focus can be used by accessibility services to select any visible user 413 interface element and act on it. This focus type is different from the more well known <em>Input 414 Focus</em>, which determines what on-screen user interface element receives input when a user 415 types characters, presses <strong>Enter</strong> on a keyboard or pushes the center button of a 416 D-pad control.</p> 417 418 <p>Accessibility Focus is completely separate and independent from Input Focus. In fact, it is 419 possible for one element in a user interface to have Input Focus while another element has 420 Accessibility Focus. The purpose of Accessibility Focus is to provide accessibility services with 421 a method of interacting with any visible element on a screen, regardless of whether or not the 422 element is input-focusable from a system perspective. You can see accessibility focus in action by 423 testing accessibility gestures. For more information about testing this feature, see 424 <a href="{@docRoot}tools/testing/testing_accessibility.html#test-gestures">Testing gesture 425 navigation</a>.</p> 426 427 <p class="note"> 428 <strong>Note:</strong> Accessibility services that use Accessibility Focus are responsible for 429 synchronizing the current Input Focus when an element is capable of this type of focus. Services 430 that do not synchronize Input Focus with Accessibility Focus run the risk of causing problems in 431 applications that expect input focus to be in a specific location when certain actions are taken. 432 </p> 433 434 <p>An accessibility service can determine what user interface element has Input Focus or 435 Accessibility Focus using the {@link android.view.accessibility.AccessibilityNodeInfo#findFocus 436 AccessibilityNodeInfo.findFocus()} method. You can also search for elements that can be selected 437 with Input Focus using the 438 {@link android.view.accessibility.AccessibilityNodeInfo#focusSearch focusSearch()} method. 439 Finally, your accessibility service can set Accessibility Focus using the 440 {@link android.view.accessibility.AccessibilityNodeInfo#performAction 441 performAction(AccessibilityNodeInfo.ACTION_SET_ACCESSIBILITY_FOCUS)} method.</p> 442 443 444 <h2 id="examples">Example Code</h2> 445 446 <p>The API Demo project contains two samples which can be used as a starting point for generating 447 accessibility services 448 ({@code <sdk>/samples/<platform>/ApiDemos/src/com/example/android/apis/accessibility}): 449 </p> 450 451 <ul> 452 <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/accessibility/ClockBackService.html">ClockBackService</a> 453 - This service is based on the original implementation of {@link 454 android.accessibilityservice.AccessibilityService} and can be used as a base for developing basic 455 accessibility services that are compatible with Android 1.6 (API Level 4) and higher.</li> 456 <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/accessibility/TaskBackService.html">TaskBackService</a> 457 - This service is based on the enhanced accessibility APIs introduced in Android 4.0 (API Level 458 14). However, you can use the Android <a href="{@docRoot}tools/support-library/index.html">Support 459 Libary</a> to substitute classes introduced in later API levels (e.g., 460 {@link android.view.accessibility.AccessibilityRecord}, 461 {@link android.view.accessibility.AccessibilityNodeInfo} 462 ) with equivalent support package classes (e.g., 463 {@link android.support.v4.view.accessibility.AccessibilityRecordCompat}, 464 {@link android.support.v4.view.accessibility.AccessibilityNodeInfoCompat} 465 ) to make this example work with API versions back to Android 1.6 (API Level 4).</li> 466 </ul> 467