在提到Handler的時候就不得不提到Looper、MessageQueue這兩個對象,到底這些對象和所謂的線程是什么關系呢?它們之間是如何聯系在一起的呢?又是如何做到發送消息的呢?這里就是要說明這三個問題的,如果你對這些問題也不能很好的回答出來,那就請繼續往下看。
這里的思路是從ActivityThread(也就是UI線程)的Looper對象開始,講這三個對象之間的關系。 首先看ActivityThread類的main方法:
// android.app.ActivityThread.javapublic static void main(String[] args) { ... // 前面省略若干 Looper.PRepareMainLooper(); ActivityThread thread = new ActivityThread(); thread.attach(false); ... // 中間省略若干 Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited");}這里在main方法中首先調用了Looper.prepareMainLooper()方法,然后在main方法的結尾調用了Looper.loop();這里我們先來看看Looper.prepareMainLooper()做了哪些操作。
// android.os.Looper.javapublic static void prepareMainLooper() { prepare(false); synchronized (Looper.class) { if (sMainLooper != null) { throw new IllegalStateException("The main Looper has already been prepared."); } sMainLooper = myLooper(); }}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));}從這里可以看到在prepare方法其實就是創建了一個Looper對象,然后綁定到了ThreadLocal對象上,而ThreadLocal對象又是線程綁定的,因此,Looper對象也被綁定到了UI線程上面。然后繼續看Looper對構造方法:
// android.os.Looper.java private Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); mThread = Thread.currentThread();}可以看到在Looper的構造方法中創建了一個MessageQueue對象,至此,Looper、MessageQueue對象是如何關聯的已經很清楚了??偨Y一下,Thread會有一個ThreadLocal對象,ThreadLocal又持有一個Looper對象,Looper又持有一個MessageQueue對象。就是這樣。。。至少沒有我之前想象的復雜。 然而,Handler和它們是如何聯系起來的呢?這個問題我們還是先不去說,先來看Looper是如何處理消息的,這里來看Looper.loop()方法:
// android.os.Looper.javapublic static void loop() { final Looper me = myLooper(); final MessageQueue queue = me.mQueue; for (;;) { Message msg = queue.next(); // might block if (msg == null) { return; } try { msg.target.dispatchMessage(msg); } finally { msg.recycleUnchecked(); } }這里省略很多無關的代碼,可以看到,當執行到Looper.loop()方法的時候,這里會進入一個死循環,MessageQueue.next()方法是阻塞式的,所以當有消息的時候就會立即處理,沒有消息的時候就會一直等待。 處理消息最終會調用到Message.target.dispatchMessage()方法,這里的target其實是一個Handler對象,所以最終處理消息是交給了Handler.diapatchMessage()方法去處理。
// android.os.Handler.javapublic void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); }}private static void handleCallback(Message message) { message.callback.run();}這里的msg.callback其實是一個Runnable對象,如果存在的話,會直接調用它的run()方法。Handler.post()、Handler.postDelay()會最終調用到這里。 另外兩個調用也很明顯,如果Handler設置了CallBack對象,則會首先調用callback對象的handleMessage方法,如果該方法沒有消費掉(就是沒有返回true)Message對象,才會調用到Handler.handleMessage()方法。 說到這里,才開始說起Handler對象,通俗的講,Handler對象只是封裝了消息的發送和接收相關的方法。這里從創建Handler對象開始。當用戶從一個線程開始創建Handler對象的時候,會最終調用到這個構造方法:
// android.os.Handler.javapublic Handler(Callback callback, boolean async) { mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( "Can't create handler inside thread that has not called Looper.prepare()"); } mQueue = mLooper.mQueue; mCallback = callback; mAsynchronous = async;}看到這里,Handler對象在創建的時候會持有當前線程Looper和MessageQueue的引用,再看sendMessage,sendMessage會最終調用到
// android.os.Handler.javaprivate boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { msg.target = this; if (mAsynchronous) { msg.setAsynchronous(true); } return queue.enqueueMessage(msg, uptimeMillis);}當添加到消息隊列后,就等待loop方法里面的訓話去處理吧。。。
新聞熱點
疑難解答