【adb 命令--进阶篇】

文章目录

    • [adb 连接设备的常用命令和DOS常用命令(2021-02-10更新 --基础篇)](https://blog.csdn.net/da_caoyuan/article/details/52879895?spm=1001.2014.3001.5501)
    • 一:获取控件属性
      • 1:获取当前顶层activity 的界面元素,包含控件属性,并且可直接从输入流中读取:
      • 2:获取当前顶层activity 的界面元素,包含控件属性,读取输入流,包含文件存储路径。
      • 3:获取当前顶层activity 的界面元素。未发现其实际用途。输入流不包含控件属性。
    • 二:解析控件数据
      • 依赖方式:
      • 具体代码:
    • \[uiautomator篇\]\[11\]下载dump,解析xml的方法
    • 解析xml文件的工具库: [dom4j](https://github.com/dom4j/dom4j)

adb 连接设备的常用命令和DOS常用命令(2021-02-10更新 --基础篇)

一:获取控件属性

1:获取当前顶层activity 的界面元素,包含控件属性,并且可直接从输入流中读取:

private void getViewElements() throws IOException {
   Process p = Runtime.getRuntime().exec("adb shell uiautomator dump --compressed && cat /sdcard/window_dump.xml");
    //取得命令结果的输出流
    InputStream in = p.getInputStream();
    //用一个读输出流类去读
    InputStreamReader isr = new InputStreamReader(in);
    //用缓冲器读行
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    StringBuilder sb = new StringBuilder();
    //直到读完为止
    while ((line = br.readLine()) != null) {
      System.out.println("ypktest=>>" + line);
      sb.append(line);
    }
  }

输入流读取的内容:
图1

2:获取当前顶层activity 的界面元素,包含控件属性,读取输入流,包含文件存储路径。

private void getViewElements() throws IOException {
    Process p = Runtime.getRuntime().exec("adb shell uiautomator dump");
    //取得命令结果的输出流
    InputStream in = p.getInputStream();
    //用一个读输出流类去读
    InputStreamReader isr = new InputStreamReader(in);
    //用缓冲器读行
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    StringBuilder sb = new StringBuilder();
    //直到读完为止
    while ((line = br.readLine()) != null) {
      System.out.println("ypktest=>>" + line);
      sb.append(line);
    }
  }


输入流读取的内容:
在这里插入图片描述
去该路径下会有window_dump.xml 文件,用IE浏览器打开效果:
在这里插入图片描述

3:获取当前顶层activity 的界面元素。未发现其实际用途。输入流不包含控件属性。

private void getViewElements() throws IOException {
    /Process p = Runtime.getRuntime().exec("adb shell dumpsys activity top");
    //取得命令结果的输出流
    InputStream in = p.getInputStream();
    //用一个读输出流类去读
    InputStreamReader isr = new InputStreamReader(in);
    //用缓冲器读行
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    StringBuilder sb = new StringBuilder();
    //直到读完为止
    while ((line = br.readLine()) != null) {
      System.out.println("ypktest=>>" + line);
      sb.append(line);
    }
  }

输入流读取的内容:
【adb 命令--进阶篇】_第1张图片

二:解析控件数据

GitHub地址

依赖方式:

Apache Maven

<dependency>
  <groupId>org.dom4j</groupId>
  <artifactId>dom4j</artifactId>
  <version>2.1.3</version>
</dependency>

Gradle方式:

implementation 'org.dom4j:dom4j:2.1.3'

具体代码:


import org.dom4j.Element;

/**
 * 数据实体类
 *
 */
public class XMLBean {

  public final Element element;
  public final int index;
  public final String text;
  public final String resourceId;
  public final String classText;
  public final String packageText;
  public final String contentDesc;
  public final boolean checkable;
  public final boolean checked;
  public final boolean clickable;
  public final boolean enable;
  public final boolean focusable;
  public final boolean focused;
  public final boolean scrollable;
  public final boolean long_clickable;
  public final boolean password;
  public final boolean selected;
  public final String bounds;

  public XMLBean(Element element) {
    this.element = element;

    index = Integer.parseInt(element.attributeValue("index"));
    text = element.attributeValue("text");
    resourceId = element.attributeValue("resource-id");
    classText = element.attributeValue("class");
    packageText = element.attributeValue("package");
    contentDesc = element.attributeValue("content-desc");
    checkable = Boolean.parseBoolean(element.attributeValue("checkable"));
    checked = Boolean.parseBoolean(element.attributeValue("checked"));
    clickable = Boolean.parseBoolean(element.attributeValue("clickable"));
    enable = Boolean.parseBoolean(element.attributeValue("enabled"));
    focusable = Boolean.parseBoolean(element.attributeValue("focusable"));
    focused = Boolean.parseBoolean(element.attributeValue("focused"));
    scrollable = Boolean.parseBoolean(element.attributeValue("scrollable"));
    long_clickable = Boolean.parseBoolean(element.attributeValue("long-clickable"));
    password = Boolean.parseBoolean(element.attributeValue("password"));
    selected = Boolean.parseBoolean(element.attributeValue("selected"));
    bounds = element.attributeValue("bounds");

  }

  @Override
  public String toString() {
    return "XMLBean [index=" + index + ", text=" + text + ", resourceId=" + resourceId + ", classText="
      + classText + ", packageText=" + packageText + ", contentDesc=" + contentDesc
      + ", checkable=" + checkable + ", checked=" + checked + ", clickable=" + clickable
      + ", enable=" + enable + ", focusable=" + focusable + ", focused=" + focused
      + ", scrollable=" + scrollable + ", long_clickable=" + long_clickable + ", password="
      + password + ", selected=" + selected + ", bounds=" + bounds + "]";
  }

}


  LinkedList<XMLBean> list = null;

  private String getParseData(InputStream in) {
    //File file = new File("C:\\\\Users\\\\P0164860\\\\Desktop\\\\ypk\\\\window_dump.xml");
    //File file = new File(filePath);

    try {
      list = new LinkedList<XMLBean>();
     
      SAXReader reader = new SAXReader();

      // 1、读取xml文件
      //Document document = reader.read(file);
      
      //也可以从输入流中读取,测试发现,输入流中读取数据,不要包含此内容: 
      Document document = reader.read(in);
      
      
      // 2、获取根节点
      Element rootNode = document.getRootElement();
      treeWalk(rootNode);

      System.out.println("list::" + list.toString());
      System.out.println("list.size::" + list.size());
      if (list != null)
        list.clear();
      list = null;
      System.out.println("End");

      String result;
      if (list != null && list.size() > 0) {
        result = list.get(0).text;
      } else {
        result = "list is null 没有找到结果";
      }


      System.out.println("WorkerVerticle2.getParseData result=" + result);
      return result;

    } catch (Exception e) {
      e.printStackTrace();
    }

    return "没有找到结果";
  }

  private void treeWalk(Element element) {

    for (Iterator i = element.elementIterator(); i.hasNext(); ) {
      Element elementNode = (Element) i.next();
      if (elementNode.elements().size() > 0) {
        treeWalk(elementNode);
      }
      System.out.println(elementNode.toString());
      XMLBean bean = new XMLBean(elementNode);
      
      if (bean.resourceId.equals("com.youdao.translator:id/tv_loading")) {
        System.out.println("ypk tv_loading===>" + bean.text);
        break;
      }
      //System.out.println("-------------------------------------------");
      if (bean.resourceId.equals("com.youdao.translator:id/trans_result_text")) {
        System.out.println("ypk===>" + bean.text);
        list.add(bean);
        break;
      }
      //System.out.println("-------------------------------------------");
    }

  }

参考文章:

[uiautomator篇][11]下载dump,解析xml的方法

解析xml文件的工具库: dom4j

Java dom4j生成和解析XML

安卓测试常用的 ADB 命令大全,非常全!!!!!

Android工具HierarchyViewer 代码导读(1) – 功能实现演示

Android工具HierarchyViewer 代码导读(2) – 建立Eclipse调试环境

Android工具HierarchyViewer代码导读(4) – 前台代码

你可能感兴趣的:(【android】,android,adb)