目录

AccessibilityService使用详解

http://image.catbro.cn/upload_88e168a529af9a66cde8d01ac0341391.png

定义

Accessibility services should only be used to assist users with disabilities in using Android devices and apps. They run in the background and receive 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.

Accessibility service应该只在残障人士在使用Android设备时被使用去辅助其操作。无障碍服务在后台运行并有AccessibilityEvent事件被触发时接收系统的回调操作。这些事件表示用户界面状态的一些变化,例如,焦点的改变,一个按钮被点击等。此类服务可以请求查询活动窗口内容的能力。开发一个无障碍服务程序需要扩展此类并实现其抽象方法。


生命周期

The lifecycle of an accessibility service is managed exclusively by the system and follows the established service life cycle. Starting an accessibility service is triggered exclusively by the user explicitly turning the service on in device settings. After the system binds to a service, it calls AccessibilityService#onServiceConnected(). This method can be overridden by clients that want to perform post binding setup.

无障碍服务的生命周期仅由系统管理,并遵循已建立的服务生命周期。仅在用户在设备设置中显示地打开该服务才能触发启动无障碍服务

An accessibility service stops either when the user turns it off in device settings or when it calls AccessibilityService#disableSelf().

当用户去设置也页面关闭或者调用AccessibilityService.disableSelf()时无障碍服务将被关闭。


宣言

An accessibility is declared as any other service in an AndroidManifest.xml, but it must do two things:

Specify that it handles the “android.accessibilityservice.AccessibilityService” Intent. Request the Manifest.permission.BIND_ACCESSIBILITY_SERVICE permission to ensure that only the system can bind to it. If either of these items is missing, the system will ignore the accessibility service. Following is an example declaration:

一个无障碍服务可以在AndroidManifest.xml中声明为任意服务,但是它必须做如下两件事:

  • 1、指定它去处理 “android.accessibilityservice.AccessibilityService"意图
  • 2、请求Manifest.permission.BIND_ACCESSIBILITY_SERVICE 权限以全包只有系统可以绑定它。

注意:如果缺少以上两项中的一项,系统将忽略无障碍服务,以下为实例说明:

1
2
3
4
5
6
7
<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.

一个可访问服务在仅监听特定的程序包的情况下可以被配置去接收指定类型的可访问事件,在给定的时间范围内仅从每种类习事件中获取一个,如检测窗口内容,指定设置活动等。

这是两种配置可访问性服务的方式:

  • 1、在声明服务时,在manifest中提供一个meta-data条目,一个使用一个meta-data 标签声明的服务如下所示:
1
2
3
4
5
6
 <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>
  • 2、可以随时调用AccessibilityService#setServiceInfo(AccessibilityServiceInfo)动态改变服务的配置

获取数据

检索窗口内容

A service can specify in its declaration that it can retrieve window content which is represented as a tree of AccessibilityWindowInfo and AccessibilityNodeInfo objects. Note that declaring this capability requires that the service declares its configuration via an XML resource referenced by SERVICE_META_DATA.

服务可以在声明中指定其可以检索窗口内容,这些内容yiAccessibilityWindowInfo和AccessibilityNodeInfo对象树的形式呈现。请注意,声明这些能力要求服务通过SERVICE_META_DATA引用的xml资源声明其配置。

Window content may be retrieved with AccessibilityEvent#getSource(), AccessibilityService#findFocus(int), AccessibilityService#getWindows(), or AccessibilityService#getRootInActiveWindow().

可以使用AccessibilityEvent#getSource(),AccessibilityService#findFocus(int),AccessibilityService#getWindows()或AccessibilityService#getRootInActiveWindow()来获取窗口内容。

通知策略

All accessibility services are notified of all events they have requested, regardless of their feedback type.

无论其反馈类型如何,所有的可访问行服务都可以接收其请求的所有事件的通知。

事件类型

名称 描述 备注
AccessibilityEvent#TYPE_VIEW_CLICKED 表示单击View的事件
AccessibilityEvent#TYPE_VIEW_LONG_CLICKED 表示长点击View的事件
AccessibilityEvent#TYPE_VIEW_FOCUSED 表示设置View输入焦点的事件
AccessibilityEvent#TYPE_VIEW_SELECTED 表示通常在AdapterView上下文中选择项目的事件。
AccessibilityEvent#TYPE_VIEW_TEXT_CHANGED 表示EditText的内容发生变化的事件
AccessibilityEvent#TYPE_WINDOW_STATE_CHANGED 表示更改用户界面的视觉上不同的部分的事件。 这些事件仅应从具有可访问性窗格标题的视图中调度,并用TYPE_WINDOW_CONTENT_CHANGED替换这些源。 有关更改的详细信息可从getContentChangeTypes()获得。
AccessibilityEvent#TYPE_NOTIFICATION_STATE_CHANGED Notifiction 展示的事件
AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_START 触摸手势开始事件
AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_END 触摸手势结束事件
AccessibilityEvent#TYPE_VIEW_HOVER_ENTER 按压在view上面的事件
AccessibilityEvent#TYPE_VIEW_HOVER_EXIT 按压结束的事件
AccessibilityEvent#TYPE_VIEW_SCROLLED 表示滚动视图的事件。通常不直接发送此事件类型
AccessibilityEvent#TYPE_VIEW_TEXT_SELECTION_CHANGED 表示在EditText中更改选择的事件
AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED 表示更改窗口内容的事件,更具体地说,它表示更改源于事件源的子树
AccessibilityEvent#TYPE_ANNOUNCEMENT 表示应用程序发出通知的事件
AccessibilityEvent#TYPE_GESTURE_DETECTION_START 表示开始手势检测的事件
AccessibilityEvent#TYPE_GESTURE_DETECTION_END 表示结束手势检测的事件
AccessibilityEvent#TYPE_TOUCH_INTERACTION_START 表示用户开始触摸屏幕的事件
AccessibilityEvent#TYPE_TOUCH_INTERACTION_END 表示用户触摸屏幕结束的事件
AccessibilityEvent#TYPE_VIEW_ACCESSIBILITY_FOCUSED 代表获得可访问性焦点的事件。
AccessibilityEvent#TYPE_WINDOWS_CHANGED 代表屏幕上显示的系统窗口中的事件更改。此事件类型仅应由系统调度
AccessibilityEvent#TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED 表示清除可访问性焦点的事件。

反馈类型

名称 描述 备注
AccessibilityServiceInfo#FEEDBACK_AUDIBLE 表示可听(不说)反馈
AccessibilityServiceInfo#FEEDBACK_HAPTIC 表示触觉反馈。
AccessibilityServiceInfo#FEEDBACK_SPOKEN 表示语音反馈。
AccessibilityServiceInfo#FEEDBACK_VISUAL 表示视觉反馈。
AccessibilityServiceInfo#FEEDBACK_GENERIC 表示一般反馈。
AccessibilityServiceInfo#FEEDBACK_BRAILLE 表示盲文反馈。

AccessibilityManager

System level service that serves as an event dispatch for AccessibilityEvents, and provides facilities for querying the accessibility state of the system. Accessibility events are generated when something notable happens in the user interface, for example an Activity starts, the focus or selection of a View changes etc. Parties interested in handling accessibility events implement and register an accessibility service which extends AccessibilityService.

系统级服务,该服务用作AccessibilityEvents的事件分派并提供用于查询系统的可访问性状态的工具。 可访问性事件是在用户界面中发生明显的事件时生成的,例如,Activity开始,视图的焦点或选择发生变化等。对处理可访问性事件感兴趣的各方将实现并注册扩展可访问性服务的可访问性服务。


accessibility-service xml tag

Use accessibility-service as the root tag of the XML resource that describes an AccessibilityService service, which is referenced from its AccessibilityService.SERVICE_META_DATA meta-data entry.

将accessibility-service用作描述AccessibilityService服务的XML资源的根标记,该XML资源是从其AccessibilityService.SERVICE_META_DATA元数据条目引用的。

属性列表

属性 说明 备注
android:description 相关数据的描述性文本 附值1
android:summary 项目摘要 附值3
android:settingsActivity 活动的完全限定类名称,该活动允许用户修改此服务的设置. 附值3
android:accessibilityEventTypes 附值2
android:packageNames 逗号分隔的程序包名称,此服务希望接收指定包名的应用发生的事件.
android:accessibilityFeedbackType 该服务提供的反馈类型如AccessibilityServiceInfo中所指定。
android:notificationTimeout 被发送到此服务的两次相同类型的可访问性事件之间的最短时间(以毫秒为单位) 整数(毫秒)
android:accessibilityFlags 在AccessibilityServiceInfo中指定的其他标志
android:canRetrieveWindowContent 可访问性服务是否希望能够检索活动窗口内容
android:canRequestTouchExplorationMode 设置可访问性服务是否处理能够请求触摸浏览模式,在该模式下,大声说出触摸的项目并且可以通过手势浏览UI true or false
android:canRequestEnhancedWebAccessibility 可访问性服务是否希望能够请求增强的Web可访问性增强功能 true or false
android:canRequestFilterKeyEvents 可访问性服务是否希望能够请求过滤关键事件。 true or false
android:canControlMagnification 可访问性服务是否需要控制显示放大倍数 true or false
android:canPerformGestures 可访问性服务是否希望能够执行手势 true or false
android:canRequestFingerprintGestures 可访问性服务是否希望从指纹传感器捕获手势 true or false
android:nonInteractiveUiTimeout AccessibilityManager.getRecommendedTimeoutMillis(int,int)中推荐使用的值(单位毫秒),以返回不适用于交互式控件的值。可以在运行时通过调用AccessibilityService.setServiceInfo()更改此设置。 整数(毫秒)
android:interactiveUiTimeout AccessibilityManager.getRecommendedTimeoutMillis(int,int)中使用的建议超​​时(以毫秒为单位),以返回适用于交互式控件的值 整数(毫秒)
android:animatedImageDrawable 可访问性服务目的或行为的动画图像,以帮助用户了解服务如何为他们提供帮助 附值1
android:htmlDescription 有关辅助功能服务的HTML说明,以帮助用户了解该服务如何为他们提供帮助 附值1
android:canTakeScreenshot 可访问性服务是否希望能够进行屏幕截图。 true or false

附:

  • 附值1:可能以“@[+][package:]type/name”的形式引用另一个资源,或者以“?[package:]type/name”的形式的主题属性
  • 附值2:服务希望接收的事件类型,如AccessibilityEvent.中指定的那些|必须为以下accessibilityEventTypes常量值列表中的一个或多个(以“ |”分隔)
  • 附值3:可能是字符串值,使用’\;‘将Unicode字符转义为’\ n’或’\ uxxxx’等字符;

accessibilityEventTypes常量值列表

常量 描述 备注
typeAllMask ffffffff Receives AccessibilityEvent.TYPES_ALL_MASK 和 所有事件
typeAnnouncement 4000 Receives AccessibilityEvent.TYPE_ANNOUNCEMENT events.
typeAssistReadingContext 1000000 Receives AccessibilityEvent.TYPE_ASSIST_READING_CONTEXT events.
typeContextClicked 800000 Receives AccessibilityEvent.TYPE_VIEW_CONTEXT_CLICKED events.
typeGestureDetectionEnd 80000 Receives AccessibilityEvent.TYPE_GESTURE_DETECTION_END events.
typeGestureDetectionStart 40000 Receives AccessibilityEvent.TYPE_GESTURE_DETECTION_START events.
typeNotificationStateChanged 40 Receives AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED events.
typeTouchExplorationGestureEnd 400 Receives AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END events.
typeTouchExplorationGestureStart 200 Receives AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START events.
typeTouchInteractionEnd 200000 Receives AccessibilityEvent.TYPE_TOUCH_INTERACTION_END events.
typeTouchInteractionStart 100000 Receives AccessibilityEvent.TYPE_TOUCH_INTERACTION_START events.
typeViewAccessibilityFocusCleared 10000 Receives AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED events.
typeViewAccessibilityFocused 8000 Receives AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED events.
typeViewClicked 1 Receives AccessibilityEvent.TYPE_VIEW_CLICKED events.
typeViewFocused 8 Receives AccessibilityEvent.TYPE_VIEW_FOCUSED events.
typeViewHoverEnter 80 Receives AccessibilityEvent.TYPE_VIEW_HOVER_ENTER events.
typeViewHoverExit 100 Receives AccessibilityEvent.TYPE_VIEW_HOVER_EXIT events.
typeViewLongClicked 2 Receives AccessibilityEvent.TYPE_VIEW_LONG_CLICKED events.
typeViewScrolled 1000 Receives AccessibilityEvent.TYPE_VIEW_SCROLLED events.
typeViewSelected 4 Receives AccessibilityEvent.TYPE_VIEW_SELECTED events.
typeViewTextChanged 10 Receives AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED events.
typeViewTextSelectionChanged 2000 Receives AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED events.
typeViewTextTraversedAtMovementGranularity 20000 Receives AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY events.
typeWindowContentChanged 800 Receives AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED events.
typeWindowStateChanged 20 Receives AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED events.
typeWindowsChanged 400000 Receives AccessibilityEvent.TYPE_WINDOWS_CHANGED events.