我正在尝试使用以下代码执行点击,但是here表示存在一些限制,我可以在测试中看到这一点。
但是似乎该错误是由于Rect()
不包含 X , Y 导致的,为什么每次我在不支持的位置单击(可能在示例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();
}
答案 0 :(得分:3)
好的,我已经回答了我自己的问题,AccessibilityService
的专家已经认为我正在构建恶意软件,而不喜欢帮助这类人。
显然,似乎无法逃避基于this的使用Rect()
和getBoundsInScreen()
的使用。
我还发现了另一个类似的代码,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虚拟键盘上执行点击。