Is it possible to use adb commands to click on a view by finding its ID?

前端 未结 1 1338
春和景丽
春和景丽 2020-12-14 13:12

Suppose I have an app (in debug/release build , made by me or not), which has an ID for a specific view.

Is it possible to call adb command to click on this view?

相关标签:
1条回答
  • 2020-12-14 14:05

    Using what @pskink explained in the comments above, here's how I achieved this:

    First, I ran this command:

    adb shell dumpsys activity top
    

    Then, I used this code to parse it:

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题