<span style="font-family: Arial, Helvetica, sans-serif;"></span>
这个方法也尝试过了,完全可以
// 响应AccessibilityEvent的事件,在用户操作的过程中,系统不断的发送
@Override
public 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来判断,我们当前需要点击的是button
if (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下载: 下载