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.
大致的意思是它是一个运行在后台的服务,并且能够接受到系统和用户之间的交互信息,如按钮的点击,焦点变化等,它的主要功能如下:
一、获取当前UI信息
二、监听event(用户的操作)
三、响应控件
我个人觉得以上三点其实和uiautomator非常像,一和三这两点其实就是一个脚本工具的基本功能了,当然,uiautomator的会简单而且容易上手,但AccessibilityService的速度也要比uiautomator,而且功能也更全,比如用AccessibilityService去写抢红包就会比uiautomator更合适。
好了,废话不多说,这篇先说说AccessibilityService的使用。
1、声明
需要在 AndroidManifest.xml 中声明一个Service, 但是需要配置intent-filter为android.accessibilityservice.AccessibilityService,还需要添加BIND_ACCESSIBILTY_SERVICE的权限
- <service
- android:name=".MyAccessibilityService"
- android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
- <intent-filter>
- <action android:name="android.accessibilityservice.AccessibilityService" />
- </intent-filter>
- <meta-data
- android:name="android.accessibilityservice"
- android:resource="@xml/accessibility" />
- </service>
2、配置
用来指定辅助事件的类型,以及监听包名等。
有两种方式,一种是通过xml来指定,这种方式需要在Service中添加meta-data属性
- <?xml version="1.0" encoding="UTF-8"?>
- <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
- android:accessibilityEventTypes="typeViewClicked|typeViewFocused"
- android:accessibilityFeedbackType="feedbackSpoken"
- android:canRetrieveWindowContent="true"
- android:notificationTimeout="100" />
还有一种方式是通过setServiceInfo方法去配置,一般都会在onServiceConneted(在服务连接成功的时候调用)方法中去调用它。如:
- <pre name="code" class="java">@Override
- protected void onServiceConnected() {
- Log.i(TAG, "config success!");
- AccessibilityServiceInfo accessibilityServiceInfo = new AccessibilityServiceInfo();
- //指定包名
- accessibilityServiceInfo.packageNames = PACKAGES;
- //指定事件类型
- accessibilityServiceInfo.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
- accessibilityServiceInfo.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
- accessibilityServiceInfo.notificationTimeout = 1000;
- setServiceInfo(accessibilityServiceInfo);
- }
3、编写辅助Service
- import android.accessibilityservice.AccessibilityService;
- import android.accessibilityservice.AccessibilityServiceInfo;
- import android.util.Log;
- import android.view.accessibility.AccessibilityEvent;
- public class MyAccessibilityService extends AccessibilityService {
- private static final String TAG = MyAccessibilityService.class.getName();
- private String[] PACKAGES = {"com.android.mms"};
- public MyAccessibilityService() {
- }
- @Override
- protected void onServiceConnected() {
- Log.i(TAG, "config success!");
- AccessibilityServiceInfo accessibilityServiceInfo = new AccessibilityServiceInfo();
- //指定包名
- accessibilityServiceInfo.packageNames = PACKAGES;
- //指定事件类型
- accessibilityServiceInfo.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
- accessibilityServiceInfo.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
- accessibilityServiceInfo.notificationTimeout = 1000;
- setServiceInfo(accessibilityServiceInfo);
- }
- @Override
- public void onAccessibilityEvent(AccessibilityEvent event) {
- int eventType = event.getEventType();
- String eventText = "";
- Log.i(TAG, "==============Start====================");
- switch (eventType) {
- case AccessibilityEvent.TYPE_VIEW_CLICKED:
- eventText = "TYPE_VIEW_CLICKED";
- break;
- case AccessibilityEvent.TYPE_VIEW_FOCUSED:
- eventText = "TYPE_VIEW_FOCUSED";
- break;
- case AccessibilityEvent.TYPE_VIEW_LONG_CLICKED:
- eventText = "TYPE_VIEW_LONG_CLICKED";
- break;
- case AccessibilityEvent.TYPE_VIEW_SELECTED:
- eventText = "TYPE_VIEW_SELECTED";
- break;
- case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
- eventText = "TYPE_VIEW_TEXT_CHANGED";
- break;
- case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
- eventText = "TYPE_WINDOW_STATE_CHANGED";
- break;
- case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
- eventText = "TYPE_NOTIFICATION_STATE_CHANGED";
- break;
- case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END:
- eventText = "TYPE_TOUCH_EXPLORATION_GESTURE_END";
- break;
- case AccessibilityEvent.TYPE_ANNOUNCEMENT:
- eventText = "TYPE_ANNOUNCEMENT";
- break;
- case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START:
- eventText = "TYPE_TOUCH_EXPLORATION_GESTURE_START";
- break;
- case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER:
- eventText = "TYPE_VIEW_HOVER_ENTER";
- break;
- case AccessibilityEvent.TYPE_VIEW_HOVER_EXIT:
- eventText = "TYPE_VIEW_HOVER_EXIT";
- break;
- case AccessibilityEvent.TYPE_VIEW_SCROLLED:
- eventText = "TYPE_VIEW_SCROLLED";
- break;
- case AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED:
- eventText = "TYPE_VIEW_TEXT_SELECTION_CHANGED";
- break;
- case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
- eventText = "TYPE_WINDOW_CONTENT_CHANGED";
- break;
- }
- eventText = eventText + ":" + eventType;
- Log.i(TAG, eventText);
- Log.i(TAG, "=============END=====================");
- }
- @Override
- public void onInterrupt() {
- }
- }
最后,在设置--辅助功能中 将对应的service开启即可,这个例子是用来监听信息应用的。