假设我有一个应用程序(在调试/发布版本中,由我自己制作),它具有特定视图的ID。
是否可以调用adb命令单击此视图?
我知道可以点击特定的坐标,但是可以使用ID吗?
我问这个是因为我知道"布局检查员"工具(可通过Android Studio获得)和"查看层次结构"工具(可通过" Android设备监视器"以前通过DDMS使用)可以显示视图的ID(甚至是它们的坐标和边界框),因此也许它可以是在执行时模拟触摸的更好方法一些自动测试。
如果需要,我可以使用root方法。
编辑:我设置了一笔赏金,以防万一我在自己的答案中写的更简单/更好的方式,就是解析" adb shell dumpsys活动的结果顶部" 。
我想知道是否有可能获得屏幕上显示的视图坐标(以及当然的大小),包括有关它们的更多信息(以识别每个)。 这也应该可以通过设备实现。也许某些东西具有相同的输出数据,可以从"监视器"工具:
注意它如何获取视图的基本信息,包括文本,id和每个视图的边界。
正如我所读到的,这可能是通过AccessibilityService实现的,但遗憾的是我无法理解它是如何工作的,它的功能是什么,如何触发它,它的要求是什么等等。
答案 0 :(得分:5)
使用上面评论中解释的@pskink,以下是我如何实现这一目标:
首先,我运行了这个命令:
adb shell dumpsys activity top
然后,我用这段代码解析它:
public class ViewCoordsGetter {
public static Rect getViewBoundyingBox(String viewIdStr) {
final List<String> viewHierarchyLog = //result of the command
for (int i = 0; i < viewHierarchyLog.size(); ++i) {
String line = viewHierarchyLog.get(i);
if (line.contains(":id/" + viewIdStr + "}")) {
Rect result = getBoundingBoxFromLine(line);
if (i == 0)
return result;
int currentLineStart = getStartOfViewDetailsInLine(line);
for (int j = i - 1; j >= 0; --j) {
line = viewHierarchyLog.get(j);
if ("View Hierarchy:".equals(line.trim()))
break;
int newLineStart = getStartOfViewDetailsInLine(line);
if (newLineStart < currentLineStart) {
final Rect boundingBoxFromLine = getBoundingBoxFromLine(line);
result.left += boundingBoxFromLine.left;
result.right += boundingBoxFromLine.left;
result.top += boundingBoxFromLine.top;
result.bottom += boundingBoxFromLine.top;
currentLineStart = newLineStart;
}
}
return result;
}
}
return null;
}
private static int getStartOfViewDetailsInLine(String s) {
int i = 0;
while (true)
if (s.charAt(i++) != ' ')
return --i;
}
private static Rect getBoundingBoxFromLine(String line) {
int endIndex = line.indexOf(',', 0);
int startIndex = endIndex - 1;
while (!Character.isSpaceChar(line.charAt(startIndex - 1)))
--startIndex;
int left = Integer.parseInt(line.substring(startIndex, endIndex));
startIndex = endIndex + 1;
endIndex = line.indexOf('-', startIndex);
endIndex = line.charAt(endIndex - 1) == ',' ? line.indexOf('-', endIndex + 1) : endIndex;
int top = Integer.parseInt(line.substring(startIndex, endIndex));
startIndex = endIndex + 1;
endIndex = line.indexOf(',', startIndex);
int right = Integer.parseInt(line.substring(startIndex, endIndex));
startIndex = endIndex + 1;
//noinspection StatementWithEmptyBody
for (endIndex = startIndex + 1; Character.isDigit(line.charAt(endIndex)); ++endIndex)
;
int bot = Integer.parseInt(line.substring(startIndex, endIndex));
return new Rect(left, top, right, bot);
}
}