老李推荐:第14章4节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-端口转发

在初始化HierarchyViewer的实例过程中,HierarchyViewer会调用自己的成员方法setupViewServer来把ViewServer装备好,那么我们这里先看下这个方法:

39     private void setupViewServer() {  

40         DeviceBridge.setupDeviceForward(mDevice);  

41         if (!DeviceBridge.isViewServerRunning(mDevice)) {  

42             if (!DeviceBridge.startViewServer(mDevice)) {  

43                 // TODO: Get rid of this delay.  

44                 try {  

45                     Thread.sleep(2000);  

46                 } catch (InterruptedException e) {  

47                 }  

48                 if (!DeviceBridge.startViewServer(mDevice)) {  

49                     Log.e(TAG, "Unable to debug device " + mDevice);  

50                     throw new RuntimeException("Could not connect to the view server");  

51                 }  

52                 return;  

53             }  

54         }  

55         DeviceBridge.loadViewServerInfo(mDevice);  

56     }  

代码14-4-1 HierarchyViewer-setupViewServer