第81-86, the body of the entire method is the thread that creates a "Device List Monitor". The thread run method runs a direct call to Devicemonitor's Devicemonitorloop method for an infinite loop to monitor the state of the device.
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;
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}
$ catch (Asynchronouscloseexception ace) {}catch (timeoutexception IoE)
201 {
202 Handleexpectioninmonitorloop (IoE);
203} catch (IOException IoE) {
204 Handleexpectioninmonitorloop (IOE);
205}
206} while (!this.mquit);
207}
Code 8-4-3 Devicemonitor-devicemonitorloop
- The first step: 163 lines, if you do not have a connection to the ADB server, the first connection
- Step Two: 168 lines, make sure the ADB server is started
- Step three: 183-185 lines, send a monitoring command to the ADB server, monitor all connected device removal
- Fourth step: processing the list of monitored devices obtained
Let's look at the first step, in the previous section we have seen the launch of the ADB server, but we have not seen how the ADB client is connected to the server, the code is an example:
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}
The catch (IOException e) {}
265
266 return adbchannel;
267}
Code 8-4-4 devicemonitor-openadbconnection
Lao Li Recommendation: 8th Chapter 4, "Monkeyrunner Source Analysis" Monkeyrunner start running Process-start Androiddebugbridge 2