老李推荐: 第8章4节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-启动AndroidDebugBridge 2...
第81-86行,整個方法的主體就是創(chuàng)建一個”Device List Monitor”的線程。線程運行方法run直接調(diào)用DeviceMonitor的deviceMonitorLoop方法來進(jìn)行無限循環(huán)監(jiān)控設(shè)備狀態(tài)了。
155?? private void deviceMonitorLoop()
156?? {
157???? do
158???? {
159?????? try
160?????? {
161???????? if (this.mMainAdbConnection == null) {
162?????????? Log.d("DeviceMonitor", "Opening adb connection");
163?????????? this.mMainAdbConnection =
openAdbConnection();
164?????????? if (this.mMainAdbConnection == null) {
165???????????? this.mConnectionAttempt += 1;
166???????? ????Log.e("DeviceMonitor", "Connection attempts: " + this.mConnectionAttempt);
167???????????? if (this.mConnectionAttempt > 10) {
168?????????????? if (!this.mServer.startAdb()) {
169???????????????? this.mRestartAttemptCount += 1;
170???????????????? Log.e("DeviceMonitor", "adb restart attempts: " + this.mRestartAttemptCount);
171?????????????? }
172?????????????? else {
173???????????????? this.mRestartAttemptCount = 0;
174?????????????? }
175???????????? }
176???????????? waitABit();
177?????????? } else {
178???????????? Log.d("DeviceMonitor", "Connected to adb for device monitoring");
179???????????? this.mConnectionAttempt = 0;
180?????????? }
181???????? }
182
183???????? if ((this.mMainAdbConnection != null) &&
(!this.mMonitoring)) {
184?????????? this.mMonitoring = sendDeviceListMonitoringRequest();
185???????? }
186
187???????? if (this.mMonitoring)
188???????? {
189?????????? int length = readLength(this.mMainAdbConnection, this.mLengthBuffer);
190
191?????????? if (length >= 0)
192?????????? {
193???????????? processIncomingDeviceData(length);
194
195
196???????????? this.mInitialDeviceListDone = true;
197?????????? }
198???????? }
199?????? }
200?????? catch (AsynchronousCloseException ace) {}catch (TimeoutException ioe)
201?????? {
202???????? handleExpectionInMonitorLoop(ioe);
203?????? } catch (IOException ioe) {
204???????? handleExpectionInMonitorLoop(ioe);
205?????? }
206???? } while (!this.mQuit);
207?? }
代碼8-4-3 DeviceMonitor - deviceMonitorLoop
?
- 第一步:163行,如果還沒有連接上的ADB服務(wù)器的話就先連接上
- 第二步: 168行,確保ADB服務(wù)器已經(jīng)啟動
- 第三步:?183-185行,往ADB服務(wù)器發(fā)送監(jiān)控命令,監(jiān)控所有連接上來的移除的設(shè)備
- 第四步: 處理所獲得的監(jiān)控設(shè)備列表
我們先看第一步,在上一節(jié)中我們已經(jīng)看到ADB服務(wù)器的啟動過程了,但是我們還沒有看到ADB客戶端是怎么連接上服務(wù)器的,一下的代碼就是一個實例:
255?? private SocketChannel openAdbConnection()
256?? {
257?? ??Log.d("DeviceMonitor", "Connecting to adb for Device List Monitoring...");
258
259???? SocketChannel adbChannel = null;
260???? try {
261?????? adbChannel =
SocketChannel.open(
AndroidDebugBridge.getSocketAddress());
262?????? adbChannel.socket().setTcpNoDelay(true);
263???? }
264???? catch (IOException e) {}
265
266???? return adbChannel;
267?? }
代碼8-4-4 DeviceMonitor - openAdbConnection
?
轉(zhuǎn)載于:https://www.cnblogs.com/poptest/p/5089490.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的老李推荐: 第8章4节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-启动AndroidDebugBridge 2...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dirname和basename命令
- 下一篇: 项目规范