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

<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下载: 下载

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值