gpt4 book ai didi

java - AccessibilityNodeInfo in getRoot ActiveWindow : Perform click fails with Rect() not contains X, Y坐标

转载 作者:搜寻专家 更新时间:2023-11-01 07:42:49 24 4
gpt4 key购买 nike

我正在尝试使用以下代码执行点击,但是 here说存在一些限制,我可以在我的测试中看到这一点。

但似乎这个错误是因为 Rect() 不包含 X, Y 坐标引起的,为什么每次我点击一个地方在不支持的地方(可能有一些限制,就像在示例的自述文件中所说的那样)在 findSmallestNodeAtPoint() 例程中的这一行执行:

if (!bounds.contains(x, y)) {
System.out.println("ERROR DETECTED!!! :::::: NOT bounds.contains(x, y) :::::::");
return null;
}

Here在这个问题中,也提到了 Github 的这个例子,并给出了一个代码示例的答案,该代码示例 100%(测试)来自 android Nougat+(api 24 及更高版本),但在我的情况下,我还需要将下面的代码修复到我的应用程序支持在旧版本的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 的专家已经认为我正在构建恶意软件而不是帮助这类人。


显然似乎不可能使用基于 thisRect()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 虚拟键盘上无法执行点击。

关于java - AccessibilityNodeInfo in getRoot ActiveWindow : Perform click fails with Rect() not contains X, Y坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52022519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com