zoukankan      html  css  js  c++  java
  • 第14章5节《MonkeyRunner源代码剖析》 HierarchyViewer实现原理-装备ViewServer-查询ViewServer执行状态

    上一小节我们描写叙述了HierarchyViewer是怎样组建ADB协议命令来实现ViewServer的port转发的。在port转发设置好后,下一个要做的事情就是去检測目标设备端ViewServer线程是否已经启动起来了。我们进入setupViewServer调用的DeviceBridge的isViewServerRunning方法:

    165     public static boolean isViewServerRunning(IDevice device) {
    166         final boolean[] result = new boolean[1];
    167         try {
    168             if (device.isOnline()) {
    169                 device.executeShellCommand(buildIsServerRunningShellCommand(),
    170                         new BooleanResultReader(result));
    171                 if (!result[0]) {
    172                     ViewServerInfo serverInfo = loadViewServerInfo(device);
    173                     if (serverInfo != null && serverInfo.protocolVersion > 2) {
    174                         result[0] = true;
    175                     }
    176                 }
    177             }
    178         } catch (TimeoutException e) {
                   ...
    187         }
    188         return result[0];
    189     }
    代码14-5-1 DeviceBridge - isViewServerRunning

    关键代码是上面的169行,通过Device类的实例来往ADB服务器发送对应的命令来检測ViewServer是否已经在执行。

    device.executeShellCommand在前面章节已经分析过了,就是用来发送”adb shell”命令的。

    我们看下buildIsServerRunningShellCommand方法。看这个命令是怎样组织起来的:

    235     private static String buildIsServerRunningShellCommand() {
    236         return String.format("service call window %d", SERVICE_CODE_IS_SERVER_RUNNING); 
    237     }
    代码14-5-2 DeviceBridge - buildIsServerRunningShellCommand

    而全局变量 SERVICE_CODE_IS_SERVER_RUNNING 的定义是:

     48     private static final int DEFAULT_SERVER_PORT = 4939;
     49     // These codes must match the auto-generated codes in IWindowManager.java
     50     // See IWindowManager.aidl as well
     51     private static final int SERVICE_CODE_START_SERVER = 1;
     52     private static final int SERVICE_CODE_STOP_SERVER = 2;
     53     private static final int SERVICE_CODE_IS_SERVER_RUNNING = 3;
    代码14-5-3 DeviceBridge - 全局变量演示样例

    236行整出来的这一串不就是”service call window 3”嘛。

    所以结合device.sendShellCommand,其实就是往设备发送了命令”adb shell service call window 3”,上一章我们才用它来查询ViewServer的执行状态了!

    注:很多其它文章请关注公众号:techgogogo或个人博客http://techgogogo.com。当然,也很欢迎您直接微信(zhubaitian1)勾搭。本文由天地会珠海分舵原创。转载请自觉,是否投诉维权看心情。


  • 相关阅读:
    阿里云ECS服务器安装docker
    ActiveMq-常见的问题
    ActiveMq-基础知识
    java语言概述
    【日语词汇类编】自然与人:气候与环境
    【日语词汇类编】传媒与娱乐:大众传播媒介
    机器学习数学基础-线性代数
    掌握机器学习数学基础之概率统计
    机器学习理论篇:机器学习的数学基础
    计算机基础知识
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7258572.html
Copyright © 2011-2022 走看看