我正在尝试使用以下代码执行单击,但here说存在一些限制,我可以在我的测试中看到这一点。
但似乎这个错误是因为Rect()
不包含X,Y coordenates,为什么每次我点击一个不支持的地方(可能是一些限制,就像在示例上的自述文件中说的那样)在findSmallestNodeAtPoint()
例程中执行此行:
if (!bounds.contains(x, y)) {
System.out.println("ERROR DETECTED!!! :::::: NOT bounds.contains(x, y) :::::::");
return null;
}
Here在这个问题中,也提到了这个Github的例子,并给出了一个代码示例,从Android Nougat +(api 24及以上)100%(测试)工作的答案,但在我的情况下我需要修复下面的代码到我的应用程序还支持在以前版本的android中执行单击。
然后已经知道这个代码失败的地方,我想知道可以做什么相关:
!bounds.contains(x, y)
代码的主要部分是:
private static void logNodeHierachy(AccessibilityNodeInfo nodeInfo, int depth) {
Rect bounds = new Rect();
nodeInfo.getBoundsInScreen(bounds);
StringBuilder sb = new StringBuilder();
if (depth > 0) {
for (int i=0; i<depth; i++) {
sb.append(" ");
}
sb.append("\u2514 ");
}
sb.append(nodeInfo.getClassName());
sb.append(" (" + nodeInfo.getChildCount() + ")");
sb.append(" " + bounds.toString());
if (nodeInfo.getText() != null) {
sb.append(" - \"" + nodeInfo.getText() + "\"");
}
System.out.println(sb.toString());
for (int i=0; i<nodeInfo.getChildCount(); i++) {
AccessibilityNodeInfo childNode = nodeInfo.getChild(i);
if (childNode != null) {
logNodeHierachy(childNode, depth + 1);
}
}
}
private static AccessibilityNodeInfo findSmallestNodeAtPoint(AccessibilityNodeInfo sourceNode, int x, int y) {
Rect bounds = new Rect();
sourceNode.getBoundsInScreen(bounds);
if (!bounds.contains(x, y)) {
System.out.println(":::::: NOT bounds.contains(x, y) :::::::");
return null;
}
for (int i=0; i<sourceNode.getChildCount(); i++) {
AccessibilityNodeInfo nearestSmaller = findSmallestNodeAtPoint(sourceNode.getChild(i), x, y);
if (nearestSmaller != null) {
return nearestSmaller;
}
}
return sourceNode;
}
public void click(int x, int y) {
System.out.println(String.format("Click [%d, %d]", x, y));
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
if (nodeInfo == null) return;
AccessibilityNodeInfo nearestNodeToMouse = findSmallestNodeAtPoint(nodeInfo, x, y);
if (nearestNodeToMouse != null) {
logNodeHierachy(nearestNodeToMouse, 0);
nearestNodeToMouse.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
nodeInfo.recycle();
}
好吧,我回答我自己的问题,已经是AccessibilityService
的专家认为我正在构建一个恶意软件而不是帮助这些人。
显然似乎不可能逃脱使用Rect()
和getBoundsInScreen()
基于this答案。
另外我发现了另一个类似的代码,ref:
public void click(int x, int y) {
clickAtPosition(x, y, getRootInActiveWindow());
}
public static void clickAtPosition(int x, int y, AccessibilityNodeInfo node) {
if (node == null) return;
if (node.getChildCount() == 0) {
Rect buttonRect = new Rect();
node.getBoundsInScreen(buttonRect);
if (buttonRect.contains(x, y)) {
// Maybe we need to think if a large view covers item?
node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
System.out.println("1º - Node Information: " + node.toString());
}
} else {
Rect buttonRect = new Rect();
node.getBoundsInScreen(buttonRect);
if (buttonRect.contains(x, y)) {
// Maybe we need to think if a large view covers item?
node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
System.out.println("2º - Node Information: " + node.toString());
}
for (int i = 0; i < node.getChildCount(); i++) {
clickAtPosition(x, y, node.getChild(i));
}
}
}
也有麻烦。但是比上面提到的代码链接更好:D。
在我的测试中,只有没有能够执行点击Android虚拟键盘。