Handler Looper( 六 )

<<<<< Finished to " + msg.target + " " + msg.callback);}// Make sure that during the course of dispatching the// identity of the thread wasn't corrupted.final long newIdent = Binder.clearCallingIdentity();if (ident != newIdent) {Log.wtf(TAG, "Thread identity changed from 0x"+ Long.toHexString(ident) + " to 0x"+ Long.toHexString(newIdent) + " while dispatching to "+ msg.target.getClass().getName() + " "+ msg.callback + " what=" + msg.what);}msg.recycleUnchecked();}}
注意这两段代码:
final Looper me = myLooper();final MessageQueue queue = me.mQueue;
之前说过一个线程必须有一个,这里不仅获取到了,还获取到了当前线程绑定的也就是消息队列,然后loop()方法最开始是判断当前线程是否有对象,之后进入一个死循环,在循环体内不断的从消息队列( queue)中取出消息对象,为什么这么说,看这个next()方法:
Message next() {int pendingIdleHandlerCount = -1; // -1 only during first iterationint nextPollTimeoutMillis = 0;for (;;) {. . . . . .nativePollOnce(mPtr, nextPollTimeoutMillis);// 阻塞于此. . . . . .// 获取next消息,如能得到就返回之 。final long now = SystemClock.uptimeMillis();Message prevMsg = null;Message msg = mMessages;// 先尝试拿消息队列里当前第一个消息if (msg != null && msg.target == null) {// 如果从队列里拿到的msg是个“同步分割栏”,那么就寻找其后第一个“异步消息”do {prevMsg = msg;msg = msg.next;} while (msg != null && !msg.isAsynchronous());}if (msg != null) {if (now < msg.when) {// Next message is not ready.Set a timeout to wake up when it is ready.nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);} else {// Got a message.mBlocked = false;if (prevMsg != null) {prevMsg.next = msg.next;} else {mMessages = msg.next;// 重新设置一下消息队列的头部}msg.next = null;if (false) Log.v("MessageQueue", "Returning message: " + msg);msg.markInUse();return msg;// 返回得到的消息对象}} else {// No more messages.nextPollTimeoutMillis = -1;}// Process the quit message now that all pending messages have been handled.if (mQuitting) {dispose();return null;}if (pendingIdleHandlerCount < 0&& (mMessages == null || now < mMessages.when)) {pendingIdleHandlerCount = mIdleHandlers.size();}if (pendingIdleHandlerCount <= 0) {// No idle handlers to run.Loop and wait some more.mBlocked = true;continue;}. . . . . .// 处理idle handlers部分for (int i = 0; i < pendingIdleHandlerCount; i++) {final IdleHandler idler = mPendingIdleHandlers[i];mPendingIdleHandlers[i] = null; // release the reference to the handlerboolean keep = false;try {keep = idler.queueIdle();} catch (Throwable t) {Log.wtf("MessageQueue", "IdleHandler threw exception", t);}if (!keep) {synchronized (this) {mIdleHandlers.remove(idler);}}}pendingIdleHandlerCount = 0;nextPollTimeoutMillis = 0;}}
注释已经很详细了,现在知道了哪里把消息取出来,但是还不知道消息是哪里处理的,接着上面的loop()方法的代码往下看:
msg.target.dispatchMessage(msg);
这一行很关键,字面意思都可以看出来这里是分发消息,找到源码查看一下,之前说过msg.就是与绑定的,所以在的源码里面找:
/*** Handle system messages here.*/public void dispatchMessage(Message msg) {if (msg.callback != null) {handleCallback(msg);} else {if (mCallback != null) {if (mCallback.handleMessage(msg)) {return;}}handleMessage(msg);}}
代码很简单,但是答案就快揭晓了,通过post和之类的方法把消息发出去,绕了一大圈又回到了,先别激动,看看代码到底说了什么:
if (msg.callback != null) {handleCallback(msg);
这里的msg.其实就是一个对象,可以通过查看源码发现: