AccessibilityService结合adb命令 进行模拟点击

今天发现AccessibilityService对于一些无点击事件的控件是无法点击的(不会进行事件传递)
解决方案是:
先使用AccessibilityService获取控件在屏幕的坐标位置,然后使用adb命令进行坐标点击

public void adbClick(final String id,final String tv,int delay) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
AccessibilityNodeInfo root = service.getRootInActiveWindow();
if (root == null) {
Log.d(TAG, "adb 无Root:" + id);
}
List tab = root.findAccessibilityNodeInfosByViewId(id);
if (tab == null || tab.size() == 0) {
Log.d(TAG, "adb 找不到点击控件:" + id);
return;
}
for (AccessibilityNodeInfo info : tab) {
if (info.getText() != null && tv.equals(info.getText().toString())) {
Rect rect = new Rect();
info.getBoundsInScreen(rect);
Log.d(TAG, "adb点击位置: "+rect.top+","+rect.bottom+","+rect.left+","+rect.right);
adbShell("input tap " + rect.left + " " + rect.top);
} else Log.d(TAG, "abd 找不到点击控件:" + id + "," + tv);
}
}
}, delay);
}

public void adbShell(String cmd){
    try {
        // 申请获取root权限,这一步很重要,不然会没有作用
        Process process = Runtime.getRuntime().exec("su");
        // 获取输出流
        OutputStream outputStream = process.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(
                outputStream);
        dataOutputStream.writeBytes(cmd);
        dataOutputStream.flush();
        dataOutputStream.close();
        outputStream.close();
        Log.d(TAG, "adbShell: 输出命令:"+cmd+"成功");
    } catch (Throwable t) {
        t.printStackTrace();
        Log.d(TAG, "adbShell: 输出命令:"+cmd+"失败");
    }
}

你可能感兴趣的:(AccessibilityService结合adb命令 进行模拟点击)