Looper.loop()方法里起了一个死循环,不断的判断MessageQueue中的消息是否为空,如果为空则直接return掉,然后执行queue.next()方法,点击进去
Message next() { // Return here if the message loop has already quit and been disposed. // This can happen if the application tries to restart a looper after quit // which is not supported. final long ptr = mPtr; if (ptr == 0) { return null; } int pendingIdleHandlerCount = -1; // -1 only during first iteration int nextPollTimeoutMillis = 0; for (;;) { if (nextPollTimeoutMillis != 0) { Binder.flushPendingCommands(); } nativePollOnce(ptr, nextPollTimeoutMillis); synchronized (this) { // Try to retrieve the next message. Return if found. final long now = SystemClock.uptimeMillis(); Message prevMsg = null; Message msg = mMessages; if (msg != null && msg.target == null) { // Stalled by a barrier. Find the next asynchronous message in the queue. 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); 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 first time idle, then get the number of idlers to run. // Idle handles only run if the queue is empty or if the first message // in the queue (possibly a barrier) is due to be handled in the future. 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; } if (mPendingIdleHandlers == null) { mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)]; } mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers); } // Run the idle handlers. // We only ever reach this code block during the first iteration. for (int i = 0; i < pendingIdleHandlerCount; i++) { final IdleHandler idler = mPendingIdleHandlers[i]; mPendingIdleHandlers[i] = null; // release the reference to the handler boolean keep = false; try { keep = idler.queueIdle(); } catch (Throwable t) { Log.wtf("MessageQueue", "IdleHandler threw exception", t); } if (!keep) { synchronized (this) { mIdleHandlers.remove(idler); } } } // Reset the idle handler count to 0 so we do not run them again. pendingIdleHandlerCount = 0; // While calling an idle handler, a new message could have been delivered // so go back and look again for a pending message without waiting. nextPollTimeoutMillis = 0; } }
Message的出栈操作,里面可能对线程,并发控制做了一些限制等。获取到栈顶的Message对象,然后就执行这个函数
msg.target.dispatchMessage(msg);
我么知道msg.tartget对象就是handler对象,我们点击dispatchMessage(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); } }
我们可以知道msg.callback != null的时候,就执行了handleCallback(msg)
private static void handleCallback(Message message) {
message.callback.run();
}
意味着这个Runable执行 run方法
然后还有就是也可能会执行到这里来
handleMessage(msg);
点击进去,
/**
* Subclasses must implement this to receive messages.
*/
public void handleMessage(Message msg) {
}
我们一般在主线程这样写接收消息
public Handler mHandlerCToP = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch(msg.what) { case 0: break; default: break; } } };
也就意味着把handleMessage()方法重写了,所以我们的代码,有地方发送消息,loop()不断分发消息,当收到了,如果接收到了,我们重写handleMessage就会掉到这个地方来,得到我需要的数据。
4、分析runOnUiThread方法和Handler.post方法和view的post方法
1、分析runOnUiThread()方法
我们一般在子线程调用这个方法也可以来更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
点击进去
public final void runOnUiThread(Runnable action) {
if (Thread.currentThread() != mUiThread) {
mHandler.post(action);
} else {
action.run();
}
}
再点击mHandler.post(action) 方法
public final boolean post(Runnable r)
{
return sendMessageDelayed(getPostMessage(r), 0);
}
点击进去
private static Message getPostMessage(Runnable r) {
Message m = Message.obtain();
m.callback = r;
return m;
}
看到了吗?其实最后还是到了发送消息这里,一开始我们不是分析了Looper.loop()里面的dispatchMessage()方法吗?
我么知道msg.tartget对象就是handler对象,我们点击dispatchMessage(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); } }
很明显程序,会走handleCallback(msg);所以会调到handler.java里面的这个方法
private static void handleCallback(Message message) {
message.callback.run();
}
这样就执行了这个Runnable
2、分析handler.post()方法
上面那个函数内部有handler.post()这个方法,已分析
3、分析view.post()方法
点击view.post()方法
public boolean post(Runnable action) { final AttachInfo attachInfo = mAttachInfo; if (attachInfo != null) { return attachInfo.mHandler.post(action); } // Assume that post will succeed later ViewRootImpl.getRunQueue().post(action); return true; }
可以发现其调用的就是activity中默认保存的handler对象的post方法