第14章6节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-启

在获得ViewServer的运行状态后,如果ViewServer还没有启动的话,HierarchyViewer的setupViewServer的下一步就会去启动ViewServer。调用的方法是DeviceBridge.startViewServer,我们进去看看:

190public static boolean startViewServer(IDevice device) {191return startViewServer(device, DEFAULT_SERVER_PORT);192}代码14-6-1 DeviceBridge-startViewServer

传入的参数是ddmlib的Device类的一个实例,方法很简单,直接转发给startViewServer的另一个重载函数,并且增加多一个4939(DEFAULT_SERVER_PORT)的端口号作为参数:

193public static boolean startViewServer(IDevice device, int port) {194final boolean[] result = new boolean[1];195try {196if (device.isOnline()) {197device.executeShellCommand(buildStartServerShellCommand(port),198new BooleanResultReader(result));199}200} catch (TimeoutException e) {…208}209return result[0];210}代码14-6-2 DeviceBridge-startViewServer重载

流程跟上一小节发送”adb shell service call window 3”来查询ViewServer一样,都是先去组建命令字串,然后通过Device实例把这个命令给发出去,只是命令不一样而已。这里我们看下发送的是什么命令:

229private static String buildStartServerShellCommand(int port) {230return String.format("service call window %d i32 %d", SERVICE_CODE_START_SERVER, port); //$NON-NLS-1$231}代码14-6-3 DeviceBridge – BuildStartServerShellCommand

其中 SERVICE_CODE_START_SERVER 全局变量是定义为1,所以可以看到最终组合的命令就是”service call window 1 i32 4939”最终结合Device的executeShellCommand命令就等于是发送”adb shell service call window 1 i32 4939”命令来启动ViewServer来监听4939端口了。这个命令我们在第13章也已经见识过了。

他们不计后果的彼此拥抱,握紧双手,怕天会亮,怕爱会走。

第14章6节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-启

相关文章:

你感兴趣的文章:

标签云: