- Handler
發(fā)送消息和消息處理 - MessageQueue
消息隊列,存放handler發(fā)送的消息 - Looper
每個線程只能創(chuàng)建一個Looper,用來管理線程里MessageQueue

三者關(guān)系
一個線程對應(yīng)一個Looper
Looper和MessageQueue一一對應(yīng)
一個Looper可以對應(yīng)多個handler
多個handler可以共享一個Looper和MessageQueue
Handler、Looper、Message Queue

概略圖
創(chuàng)建
Looper創(chuàng)建在ActivityThread中
public static void main(String[] args) {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
............
//創(chuàng)建Looper并存放在threadLocal中
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false);
if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
}
if (false) {
Looper.myLooper().setMessageLogging(new
LogPrinter(Log.DEBUG, "ActivityThread"));
}
// End of event ActivityThreadMain.
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
//looper開始工作
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
調(diào)用Looper.prepareMainLooper()初始化looper,prepareMainLooper調(diào)用prepare(false)方法,創(chuàng)建Looper,并設(shè)置到local thread中
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
Looper在初始化的時候在創(chuàng)建MessageQueue,這樣looper和messageQueue綁定在一起
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
調(diào)用 Looper.loop(),循環(huán)從MessageQueue獲取消息,沒有消息會阻塞
public static void loop() {
final Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue;
// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
// This must be in a local variable, in case a UI event sets the logger
Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
msg.target.dispatchMessage(msg);
if (logging != null) {
logging.println("<<<<< 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();
}
}
發(fā)送消息
Handler可以通過post或者sendMessage方法發(fā)送消息,發(fā)送的對象分別是Runnable和Message,最后都會封裝為Message消息發(fā)送
public final boolean post(Runnable r)
{
return sendMessageDelayed(getPostMessage(r), 0);
}
getPostMessage方法將Message的callback設(shè)置為Runnable。
消息處理
在looper存在靜態(tài)方法public static void loop() {},調(diào)用msg.target.dispatchMessage(msg);方法處理消息
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
- 如果msg存在callback,調(diào)用msg回調(diào)方法處理
- 如果handler存在callback,調(diào)用handler的callback方法處理
- 如果上面兩個callback不存在,調(diào)用handler方法handleMessage來處理消息