Android 辅助功能(无障碍)自定义开发类似微信抢红包插件
背景:
通常在无人值守的Android智能设备或不能触摸交互的设备中,当系统运行异常时, 系统有可能会弹出应用程序无响应(ANR)对话框,提示是否终止当前进程或等待,或应用程序异常退出时系统偶尔也会有弹窗,那么对于无人值守的设备,我们可以通过Android 辅助功能,监听到有异常时, 自动帮用户点掉窗口。辅助功能需要用到 AccessibilityService , AccessibilityService是系统提供的无障碍辅助服务接口,我们可以通过继承AccessibilityService 来实现自定义无障碍服务。
实现:
1.继承AccessibilityService
public class MyAccessibilityService extends AccessibilityService { private static final String TAG = "MyAccessibilityService"; @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate"); //从系统设置里面开启辅助功能时回调1 } @Override protected void onServiceConnected() { super.onServiceConnected(); Log.d(TAG, "onServiceConnected"); //从系统设置里面开启辅助功能时回调2 } @Override public void onAccessibilityEvent(AccessibilityEvent event) { int eventType = event.getEventType(); // 输出事件的字符串type String typeStr = event.eventTypeToString(eventType); Log.d(TAG, "typeStr:" + typeStr); //当有事件被监听到时,会回调该方法,AccessibilityEvent 里包含事件类型,发生源头等, } @Override public void onInterrupt() { Log.d(TAG, "onInterrupt"); } @Override public boolean onUnbind(Intent intent) { Log.d(TAG, "onUnbind"); return super.onUnbind(intent); //从系统辅助功能里关闭该服务时回调1 } @Override public void onDestroy() { Log.d(TAG, "onDestroy"); super.onDestroy(); //从系统辅助功能里关闭该服务时回调2 ,即服务会被销毁 }}
2.不要忘记在AndroidManifest.xml 中注册服务
>
其中
android:resource="@xml/my_service_config" />
要固定这样写,android:resource 指定一个accessibilityservice 的配置项
3. 创建 res/xml/my_service_config
<?xml version="1.0" encoding="utf-8"?>
android:accessibilityEventTypes="typeWindowStateChanged" 这里指定的是监听窗体状态变化
android:packageNames="android" 指定要监听那个进程,指定其报名, 这里我监听的是android, 也就是系统进程,ANR 是由系统进程弹出的
android:description="@string/accessibility_description" 指定的是服务的描述,就是用户在系统设置界面辅助功能列表里看到的服务描述
4.启动
安装完成后到系统设置辅助功能选项里面将服务打开,辅助服务就启动了,当你想要监听的事件有变化时,系统就会回调服务的onAccessibilityEvent方法,事件原因,类型,等都会包含在event中。
-------示例,监听系统ANR 并自动点掉对话框,其余参考上面的配置
@Override public void onAccessibilityEvent(AccessibilityEvent event) { int eventType = event.getEventType(); // 输出事件的字符串type String typeStr = event.eventTypeToString(eventType); Log.d(TAG, "typeStr:" + typeStr); AccessibilityNodeInfo nodeInfo = event.getSource(); if (nodeInfo == null) { return; } try { parserTree(nodeInfo); } catch (Exception e) { e.printStackTrace(); } } private void parserTree(AccessibilityNodeInfo node) { List buttonNode = new ArrayList<>(); if (node != null) { int childCount = node.getChildCount(); if (childCount >= 1) { for (int i = 0; i < childCount; i++) { AccessibilityNodeInfo child = node.getChild(i); parserTree(child); } } else { CharSequence text = node.getText(); if (TextUtils.isEmpty(text)) { } else { if (text.equals("等待")) { // ANR node.performAction(AccessibilityNodeInfo.ACTION_CLICK); return; } if (text.equals("取消") || text.equals("确定") || text.equals("确认")) { buttonNode.add(node); } } } } else { Log.d(TAG, "node is null"); } if (buttonNode != null & buttonNode.size() == 1) { Log.d(TAG, "命中"); buttonNode.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK); } }
应用安装好后,你也可以通过如下命令通过adb开启服务adb shell settings put secure enabled_accessibility_services com.aaa.bbb.ccc/com.aaa.bbb.ccc.MyAccessibilityServiceadb shell settings put secure accessibility_enabled 1
当然如果你的设备是root的,你也可以通过代码通过su直接开启
su settings put secure enabled_accessibility_services com.aaa.bbb.ccc/com.aaa.bbb.ccc.MyAccessibilityService
su settings put secure accessibility_enabled 1
还需要权限
说到ANR ,系统在检测到应用发生ANR时,会发出广播,如果你想做ANR监控,也可以监听这个广播来处理
参考:
https://www.jianshu.com/p/959217070c87
https://blog.csdn.net/qq_24800377/article/details/78283662
https://blog.csdn.net/seu_calvin/article/details/51912738
http://www.cnblogs.com/popfisher/archive/2017/08/30/7455754.html
更多相关文章
- 基于Android的校园快递系统_移动端设计_后台管理_MySQL数据库设
- 【生态】特斯拉的中控系统为什么是Linux而不是Android?
- 用U盘安装Android系统在PC x86平台上运行Android
- Android中触摸事件传递机制
- Android中系统触摸相关辅助类总结
- ANDROID音频系统散记之五:如何绕开多媒体音轨的重采样
- 《Android系统源代码情景分析》连载回忆录:灵感之源