在获得ViewServer的运行状态后,如果ViewServer还没有启动的话,HierarchyViewer的setupViewServer的下一步就会去启动ViewServer。调用的方法是DeviceBridge.startViewServer,我们进去看看:
190 public static boolean startViewServer(IDevice device) { 191 return startViewServer(device, DEFAULT_SERVER_PORT); 192 }代码14-6-1 DeviceBridge-startViewServer
传入的参数是ddmlib的Device类的一个实例,方法很简单,直接转发给startViewServer的另一个重载函数,并且增加多一个4939(DEFAULT_SERVER_PORT)的端口号作为参数:
193 public static boolean startViewServer(IDevice device, int port) { 194 final boolean[] result = new boolean[1]; 195 try { 196 if (device.isOnline()) { 197 device.executeShellCommand(buildStartServerShellCommand(port), 198 new BooleanResultReader(result)); 199 } 200 } catch (TimeoutException e) { ... 208 } 209 return result[0]; 210 }代码14-6-2 DeviceBridge-startViewServer重载
流程跟上一小节发送”adb shell service call window 3”来查询ViewServer一样,都是先去组建命令字串,然后通过Device实例把这个命令给发出去,只是命令不一样而已。这里我们看下发送的是什么命令:
229 private static String buildStartServerShellCommand(int port) { 230 return 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章也已经见识过了。
注:更多文章请关注公众号:techgogogo或个人博客http://techgogogo.com。当然,也非常欢迎您直接微信(zhubaitian1)勾搭。本文由天地会珠海分舵原创。转载请自觉,是否投诉维权看心情。