accessibilityservice 自动安装 取消删除安装包(二)

来源:互联网 发布:微博关键词优化 编辑:程序博客网 时间:2024/05/17 03:41
<span style="font-family: Arial, Helvetica, sans-serif;"></span>

这个方法也尝试过了,完全可以


// 响应AccessibilityEvent的事件,在用户操作的过程中,系统不断的发送@Overridepublic void onAccessibilityEvent(AccessibilityEvent event) {Log.i(TAG, "onAccessibilityEvent:" + event.toString());// 1.方法可行// checkInstall(event);// 2.方法可行int eventType = event.getEventType();AccessibilityNodeInfo accessibilityNodeInfo = event.getSource();// 得到被点击的对象getRootInActiveWindow(); // 获得整个窗口对象if (eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED|| eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {// 当当前窗口发生变化时候,重新遍历,否则后面的控件找不到AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();if (rootInActiveWindow == null) {Log.d(TAG, "the rootInActiveWindow is null");} else {recycle(rootInActiveWindow);}}// 3.方法可行// if (event.getSource() == null) {// Log.d("test", "the source = null");// } else {// Log.d("test", "event = " + event.toString());// switch (INVOKE_TYPE) {// case TYPE_KILL_APP:// // processKillApplication(event);// break;// case TYPE_INSTALL_APP:// processinstallApplication(event);// break;// case TYPE_UNINSTALL_APP:// // processUninstallApplication(event);// break;// default:// break;// }// }}

@SuppressLint("NewApi")public void recycle(AccessibilityNodeInfo rootInActiveWindow) {if (rootInActiveWindow.getChildCount() == 0) { // 只有一个子元素Log.d(TAG, "the rootInActiveWindow count is : 0");} else {for (int i = 0; i < rootInActiveWindow.getChildCount(); i++) {AccessibilityNodeInfo nodeInfo = rootInActiveWindow.getChild(i);// 这里根据UIAutomator来判断,我们当前需要点击的是buttonif (nodeInfo.getClassName().equals("android.widget.Button")) {String content = nodeInfo.getText().toString();List<AccessibilityNodeInfo> okList = nodeInfo.findAccessibilityNodeInfosByViewId("com.android.packageinstaller:id/ok_button");List<AccessibilityNodeInfo> openList = nodeInfo.findAccessibilityNodeInfosByViewId("com.android.packageinstaller:id/launch_button");if ((null != okList && okList.size() > 0)) {Log.d(TAG, "the node content is :" + content + "==the okbutton is :" + okList.get(0));// 通过performAction来实现模拟点击事件okList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);}// 安装完成之后点击open按钮,打开当前应用if (null != openList && openList.size() > 0) {Log.d(TAG, "the node content is :" + content + "==the openButton is :" + openList.get(0));openList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);}String cancelDelete = nodeInfo.getText().toString();// 这里的取消是我的荣耀手机上的是否删除软件安装包的,这里选择取消if ("取消".equals(cancelDelete)) {nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);}}}}}

demo下载:下载

1 0
原创粉丝点击