前奏:
在你的手机更多设置或者高级设置中,我们会发现有个无障碍的功能,很多人不知道这个功能具体是干嘛的,其实这个功能是为了增强用户界面以帮助残障人士,或者可能暂时无法与设备充分交互的人们
image.png
它的具体实现是通过AccessibilityService服务运行在后台中,通过AccessibilityEvent接收指定事件的回调。
例如:焦点改变了,一个按钮被点击,等等。这样的服务可以选择请求活动窗口的内容的能力。简单的说AccessibilityService就是一个后台监控服务,当你监控的内容发生改变时,就会调用后台服务的回调方法
AccessibilityService使用
1.1创建服务类
编写自己的Service类,重写onServiceConnected()方法、onAccessibilityEvent()方法和onInterrupt()方法
/**
* Created by zxn on 2019/4/29.
*/
public class PlugAccessibilityService extends AccessibilityService {
/**
* 当启动服务的时候就会被调用,系统成功绑定该服务时被触发,也就是当你在设置中开启相应的服务,
* 系统成功的绑定了该服务时会触发,通常我们可以在这里做一些初始化操作
*/
@Override
protected void onServiceConnected() {
super.onServiceConnected();
}
/**
* 通过系统监听窗口变化的回调,sendAccessibiliyEvent()不断的发送AccessibilityEvent到此处
*
* @param event
*/
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
int eventType = event.getEventType();
//根据时间回调类型进行处理.
}
/**
* 中断服务时的回调.
*/
@Override
public void onInterrupt() {
}
/**
* 查找拥有特定焦点类型的控件
*
* @param focus
* @return
*/
@Override
public AccessibilityNodeInfo findFocus(int focus) {
return super.findFocus(focus);
}
/**
* 如果配置能够获取窗口内容,则会返回当前活动窗口的根结点
*
* @return
*/
@Override
public AccessibilityNodeInfo getRootInActiveWindow() {
return super.getRootInActiveWindow();
}
/**
* 获取系统服务
*
* @param name
* @return
*/
@Override
public Object getSystemService(String name) {
return super.getSystemService(name);
}
/**
* 如果允许服务监听按键操作,该方法是按键事件的回调,需要注意,这个过程发生了系统处理按键事件之前
*
* @param event
* @return
*/
@Override
protected boolean onKeyEvent(KeyEvent event) {
return super.onKeyEvent(event);
}
}
1.1.1AccessibilityService中常用的方法的介绍
-
findFocus(int focus)
查找拥有特定焦点类型的控件 -
getRootInActiveWindow()
如果配置能够获取窗口内容,则会返回当前活动窗口的根结点 -
final void disableSelf()
禁用当前服务,也就是在服务可以通过该方法停止运行 -
final AccessibilityServiceInfo getServiceInfo()
获取当前服务的配置信息 -
onAccessibilityEvent(AccessibilityEvent event)
有关AccessibilityEvent事件的回调函数,系统通过sendAccessibiliyEvent()不断的发送AccessibilityEvent到此处 -
performGlobalAction(int action)
执行全局操作,比如返回,回到主页,打开最近等操作 -
setServiceInfo(AccessibilityServiceInfo info)
设置当前服务的配置信息 -
getSystemService(String name)
获取系统服务 -
onKeyEvent(KeyEvent event)
如果允许服务监听按键操作,该方法是按键事件的回调,需要注意,这个过程发生了系统处理按键事件之前 -
onServiceConnected()
系统成功绑定该服务时被触发,也就是当你在设置中开启相应的服务,系统成功的绑定了该服务时会触发,通常我们可以在这里做一些初始化操作 -
onInterrupt()
服务中断时的回调
1.2 声明服务
在manifests中配置该服务信息
<service
android:name=".PlugAccessibilityService"
android:enabled="true"
android:exported="true"
android:label="@string/plug_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
</intent-filter>
</service>
注意:任何一个信息配置错误,都会使该服务无反应
-
android:label:在无障碍列表中显示该服务的名字
image.png
- android:permission:需要指定BIND_ACCESSIBILITY_SERVICE权限,这是4.0以上的系统要求的
- intent-filter:这个name是固定不变的
1.3 配置服务参数
配置服务参数是指:配置用来接受指定类型的事件,监听指定package,检索窗口内容,获取事件类型的时间等等。其配置服务参数有两种方法:
-
方法一:安卓4.0之后可以通过meta-data标签指定xml文件进行配置
-
方法二:通过代码动态配置参数
1.3.1 方法一
在原先的manifests中增加meta-data标签指定xml文件
<service
android:name=".PlugAccessibilityService"
android:enabled="true"
android:exported="true"
android:label="@string/plug_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config"
/>
</service>
接下来是accessibility_service_config文件的配置
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagRequestFilterKeyEvents"
android:canRequestFilterKeyEvents="true"
android:canRetrieveWindowContent="true"
android:notificationTimeout="100" />
下面是对xml参数的介绍
accessibilityEventTypes:表示该服务对界面中的哪些变化感兴趣,即哪些事件通知,比如窗口打开,滑动,焦点变化,长按等。具体的值可以在AccessibilityEvent类中查到,如typeAllMask表示接受所有的事件通知
accessibilityFeedbackType:表示反馈方式,比如是语音播放,还是震动
canRetrieveWindowContent:表示该服务能否访问活动窗口中的内容。也就是如果你希望在服务中获取窗体内容,则需要设置其值为true
description:对该无障碍功能的描述,具体体现在下图
notificationTimeout:接受事件的时间间隔,通常将其设置为100即可
packageNames:表示对该服务是用来监听哪个包的产生的事件
1.3.2 方法二
通过代码为我们的AccessibilityService配置AccessibilityServiceInfo信息
public void setAccessibilityServiceInfo() {
String[] packageNames = {"com.tencent.mm"};
AccessibilityServiceInfo accessibilityServiceInfo = new AccessibilityServiceInfo();
//相应时间的类型,(长安,点击,滑动)
accessibilityServiceInfo.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
//反馈给用户的类型,这里是语音
accessibilityServiceInfo.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
//过滤的包名
accessibilityServiceInfo.packageNames = packageNames;
setServiceInfo(accessibilityServiceInfo);
}
在这里涉及到了AccessibilityServiceInfo类,AccessibilityServiceInfo类被用于配置AccessibilityService信息,该类中包含了大量用于配置的常量字段及用来xml属性,常见的有:accessibilityEventTypes,canRequestFilterKeyEvents,packageNames等等
1.4 启动服务
这里我们需要在无障碍功能里面手动打开该项功能,否则无法继续进行,通过
下面代码可以打开系统的无障碍功能列表
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivity(intent);
1.5 处理事件信息
由于我们监听了事件的通知栏和界面等信息,当我们指定packageNames的通知栏或者界面发生变化时,会通过onAccessibilityEvent回调我们的事件,接着进行事件的处理
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
int eventType = event.getEventType();
//根据时间回调类型进行处理.
switch (eventType) {
//通知栏变化时
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
break;
case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
//当窗口状态发生改变时.
break;
}
}
当我们微信收到通知时,状态栏会有一条推送信息到达,这个时候就会被TYPE_NOTIFICATION_STATE_CHANGED监听,执行里面的内容,当我们切换微信界面时,或者使用微信时,这个时候就会被TYPE_WINDOW_STATE_CHANGED监听,执行里面的内容
AccessibilityEvent的方法
getEventType():事件类型
getSource():获取事件源对应的结点信息
getClassName():获取事件源对应类的类型,比如点击事件是有某个Button产生的,那么此时获取的就是Button的完整类名
getText():获取事件源的文本信息,比如事件是有TextView发出的,此时获取的就是TextView的text属性。如果该事件源是树结构,那么此时获取的是这个树上所有具有text属性的值的集合
isEnabled():事件源(对应的界面控件)是否处在可用状态
getItemCount():如果事件源是树结构,将返回该树根节点下子节点的数量
1.6 获取节点信息
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
1.6.2 获取指定子节点(控件节点)
//通过文本找到对应节点集合.
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
List<AccessibilityNodeInfo> textList = nodeInfo.findAccessibilityNodeInfosByText("");
//通过空间id找到对应的节点集合.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
List<AccessibilityNodeInfo> textIdList = nodeInfo.findAccessibilityNodeInfosByViewId("000");
}
1.7 模拟节点点击
当我们获取了节点信息之后,对控件节点进行模拟点击、长按等操作,AccessibilityNodeInfo类提供了performAction()方法让我们执行模拟操作,具体操作可看官方文档介绍,这里列举常用的操作
//模拟点击
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
//模拟长安
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
//模拟获取焦点
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
//模拟粘贴.
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_PASTE);
监听控件插件实现
2.1 原理分析
2.2 注意事项
1,每个版本的按钮ID都是不一样的,在我们的程序中是需要去修改按钮ID,以达到版本的适配
2,在获取控件ID的时候,注意其布局是否可点击,否则获取不可点击的控件,会使程序无反应
2.3 获取控件ID
当我们手机接入USB线时,在Android Device Monitor中的选择设备并开启Dump View Hierarchy for UI Automator工具,通过它可以获取控件信息
monitor的安装目录.
C:\ZxnProgram\Android\Sdk\tools\monitor.bat
2.4 代码实现
科迈赢钱
包名:
com.pos.kmretailpos.mini
id:
com.pos.kmretailpos.mini:id/tv_waipay
com.pos.kmretailpos.mini:id/et_ys
科迈赢钱
包名:
com.pos.kmretailpos.mini
id:
com.pos.kmretailpos.mini:id/tv_waipay
com.pos.kmretailpos.mini:id/et_ys
AccessibilityEvent时间类型:
/**
* View被点击--->1
* Represents the event of clicking on a {@link android.view.View} like
* {@link android.widget.Button}, {@link android.widget.CompoundButton}, etc.
*/
public static final int TYPE_VIEW_CLICKED = 0x00000001;
/**
* View被长按
* Represents the event of long clicking on a {@link android.view.View} like
* {@link android.widget.Button}, {@link android.widget.CompoundButton}, etc.
*/
public static final int TYPE_VIEW_LONG_CLICKED = 0x00000002;
/**
* View被选中
* Represents the event of selecting an item usually in the context of an
* {@link android.widget.AdapterView}.
*/
public static final int TYPE_VIEW_SELECTED = 0x00000004;
/**
* View获得焦点
* Represents the event of setting input focus of a {@link android.view.View}.
*/
public static final int TYPE_VIEW_FOCUSED = 0x00000008;
/**
* View文本变化
* Represents the event of changing the text of an {@link android.widget.EditText}.
*/
public static final int TYPE_VIEW_TEXT_CHANGED = 0x00000010;
/**
* 监听窗口状态变化,比如打开一个popupWindow,dialog,Activity切换等等.打开了一个PopupWindow,Menu或Dialog(--->32)
* Represents the event of a change to a visually distinct section of the user interface.
* These events should only be dispatched from {@link android.view.View}s that have
* accessibility pane titles, and replaces {@link #TYPE_WINDOW_CONTENT_CHANGED} for those
* sources. Details about the change are available from {@link #getContentChangeTypes()}.
*/
public static final int TYPE_WINDOW_STATE_CHANGED = 0x00000020;
/**
* Notification通知变化
* Represents the event showing a {@link android.app.Notification}.
*/
public static final int TYPE_NOTIFICATION_STATE_CHANGED = 0x00000040;
/**
* 一个View进入悬停
* Represents the event of a hover enter over a {@link android.view.View}.
*/
public static final int TYPE_VIEW_HOVER_ENTER = 0x00000080;
/**
* 一个View退出悬停
* Represents the event of a hover exit over a {@link android.view.View}.
*/
public static final int TYPE_VIEW_HOVER_EXIT = 0x00000100;
/**
* 触摸浏览事件开始
* Represents the event of starting a touch exploration gesture.
*/
public static final int TYPE_TOUCH_EXPLORATION_GESTURE_START = 0x00000200;
/**
* 触摸浏览事件完成
* Represents the event of ending a touch exploration gesture.
*/
public static final int TYPE_TOUCH_EXPLORATION_GESTURE_END = 0x00000400;
/**
* 窗口的内容发生变化,或子树根布局发生变化(--->2048)
* Represents the event of changing the content of a window and more
* specifically the sub-tree rooted at the event's source.
*/
public static final int TYPE_WINDOW_CONTENT_CHANGED = 0x00000800;
/**
* View滚动
* Represents the event of scrolling a view. This event type is generally not sent directly.
* @see View#onScrollChanged(int, int, int, int)
*/
public static final int TYPE_VIEW_SCROLLED = 0x00001000;
/**
* Edittext文字选中发生改变事件
* Represents the event of changing the selection in an {@link android.widget.EditText}.
*/
public static final int TYPE_VIEW_TEXT_SELECTION_CHANGED = 0x00002000;
/**
* 应用产生一个通知事件
* Represents the event of an application making an announcement.
*/
public static final int TYPE_ANNOUNCEMENT = 0x00004000;
/**
* 获得无障碍焦点事件
* Represents the event of gaining accessibility focus.
*/
public static final int TYPE_VIEW_ACCESSIBILITY_FOCUSED = 0x00008000;
/**
* 无障碍焦点事件清除
* Represents the event of clearing accessibility focus.
*/
public static final int TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED = 0x00010000;
/**
* 在给定的移动粒度下遍历视图文本的事件
* Represents the event of traversing the text of a view at a given movement granularity.
*/
public static final int TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY = 0x00020000;
/**
* 开始手势监测
* Represents the event of beginning gesture detection.
*/
public static final int TYPE_GESTURE_DETECTION_START = 0x00040000;
/**
* 结束手势监测
* Represents the event of ending gesture detection.
*/
public static final int TYPE_GESTURE_DETECTION_END = 0x00080000;
/**
* 触摸屏幕事件开始
* Represents the event of the user starting to touch the screen.
*/
public static final int TYPE_TOUCH_INTERACTION_START = 0x00100000;
/**
* 触摸屏幕事件结束
* Represents the event of the user ending to touch the screen.
*/
public static final int TYPE_TOUCH_INTERACTION_END = 0x00200000;
/**
* 屏幕上的窗口变化事件,需要API 21+
* Represents the event change in the system windows shown on the screen. This event type should
* only be dispatched by the system.
*/
public static final int TYPE_WINDOWS_CHANGED = 0x00400000;
/**
* View中的上下文点击事件
* Represents the event of a context click on a {@link android.view.View}.
*/
public static final int TYPE_VIEW_CONTEXT_CLICKED = 0x00800000;
/**
* 辅助用户读取当前屏幕事件
* Represents the event of the assistant currently reading the users screen context.
*/
public static final int TYPE_ASSIST_READING_CONTEXT = 0x01000000;
/**
* Change type for {@link #TYPE_WINDOW_CONTENT_CHANGED} event:
* The type of change is not defined.
*/
public static final int CONTENT_CHANGE_TYPE_UNDEFINED = 0x00000000;
/**
* Change type for {@link #TYPE_WINDOW_CONTENT_CHANGED} event:
* One or more content changes occurred in the the subtree rooted at the source node,
* or the subtree's structure changed when a node was added or removed.
*/
public static final int CONTENT_CHANGE_TYPE_SUBTREE = 0x00000001;
/**
* Change type for {@link #TYPE_WINDOW_CONTENT_CHANGED} event:
* The node's text changed.
*/
public static final int CONTENT_CHANGE_TYPE_TEXT = 0x00000002;
/**
* Change type for {@link #TYPE_WINDOW_CONTENT_CHANGED} event:
* The node's content description changed.
*/
public static final int CONTENT_CHANGE_TYPE_CONTENT_DESCRIPTION = 0x00000004;
/**
* Change type for {@link #TYPE_WINDOW_STATE_CHANGED} event:
* The node's pane title changed.
*/
public static final int CONTENT_CHANGE_TYPE_PANE_TITLE = 0x00000008;
/**
* Change type for {@link #TYPE_WINDOW_STATE_CHANGED} event:
* The node has a pane title, and either just appeared or just was assigned a title when it
* had none before.
*/
public static final int CONTENT_CHANGE_TYPE_PANE_APPEARED = 0x00000010;
/**
*
* Change type for {@link #TYPE_WINDOW_STATE_CHANGED} event:
* Can mean one of two slightly different things. The primary meaning is that the node has
* a pane title, and was removed from the node hierarchy. It will also be sent if the pane
* title is set to {@code null} after it contained a title.
* No source will be returned if the node is no longer on the screen. To make the change more
* clear for the user, the first entry in {@link #getText()} will return the value that would
* have been returned by {@code getSource().getPaneTitle()}.
*/
public static final int CONTENT_CHANGE_TYPE_PANE_DISAPPEARED = 0x00000020;
/**
* 一个窗口被添加了
* Change type for {@link #TYPE_WINDOWS_CHANGED} event:
* The window was added.
*/
public static final int WINDOWS_CHANGE_ADDED = 0x00000001;
/**
* 一个被窗口移除了.
* Change type for {@link #TYPE_WINDOWS_CHANGED} event:
* A window was removed.
*/
public static final int WINDOWS_CHANGE_REMOVED = 0x00000002;
/**
* Change type for {@link #TYPE_WINDOWS_CHANGED} event:
* The window's title changed.
*/
public static final int WINDOWS_CHANGE_TITLE = 0x00000004;
/**
* Change type for {@link #TYPE_WINDOWS_CHANGED} event:
* The window's bounds changed.
*/
public static final int WINDOWS_CHANGE_BOUNDS = 0x00000008;
/**
* Change type for {@link #TYPE_WINDOWS_CHANGED} event:
* The window's layer changed.
*/
public static final int WINDOWS_CHANGE_LAYER = 0x00000010;
/**
* Change type for {@link #TYPE_WINDOWS_CHANGED} event:
* The window's {@link AccessibilityWindowInfo#isActive()} changed.
*/
public static final int WINDOWS_CHANGE_ACTIVE = 0x00000020;
/**
* Change type for {@link #TYPE_WINDOWS_CHANGED} event:
* The window's {@link AccessibilityWindowInfo#isFocused()} changed.
*/
public static final int WINDOWS_CHANGE_FOCUSED = 0x00000040;
/**
* Change type for {@link #TYPE_WINDOWS_CHANGED} event:
* The window's {@link AccessibilityWindowInfo#isAccessibilityFocused()} changed.
*/
public static final int WINDOWS_CHANGE_ACCESSIBILITY_FOCUSED = 0x00000080;
/**
* Change type for {@link #TYPE_WINDOWS_CHANGED} event:
* The window's parent changed.
*/
public static final int WINDOWS_CHANGE_PARENT = 0x00000100;
/**
* Change type for {@link #TYPE_WINDOWS_CHANGED} event:
* The window's children changed.
*/
public static final int WINDOWS_CHANGE_CHILDREN = 0x00000200;
/**
*
* Change type for {@link #TYPE_WINDOWS_CHANGED} event:
* The window either entered or exited picture-in-picture mode.
*/
public static final int WINDOWS_CHANGE_PIP = 0x00000400;
网友评论