標籤:
背景需求
在Android中,當遇到子線程需要重新整理UI時,最常的做法就是handler,當然還有其他方便的方法如Android給我們提供的runOnUiThread(runnable)方法,但歸根結底都是使用handler來重新整理UI的。
Android訊息傳遞原理
簡單的講:handler發送(post或send)一條訊息;MessageQueue(隊,實際上是一個用單鏈表實現的隊列)接受並儲存該訊息;looper無限迴圈的從MessageQueue中取出一條訊息,例如msg,然後調用msg.target.dispatchMessage(msg)方法對msg進行處理。其中msg.target為發送該msg的handler。
源碼解讀
Handler
Handler在Android訊息傳遞過程中,主要負責發送訊息和處理事件。
當handler發送一條訊息時,無論是使用handler.postXX()還是使用handler.sendXXX()最終調用的都是:
public boolean sendMessageAtTime(Message msg, long uptimeMillis) { MessageQueue queue = mQueue; if (queue == null) { RuntimeException e = new RuntimeException( this + " sendMessageAtTime() called with no mQueue"); Log.w("Looper", e.getMessage(), e); return false; } return enqueueMessage(queue, msg, uptimeMillis); }
其中enqueueMessage()方法是執行單鏈表在隊尾的插入操作,該方法主要完成的是想messagequeue中插入一條訊息。
dispatchMessage(Message msg)方法:
public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
在looper.loop()方法中,調用該方法對訊息msg進行處理。
幾點說明:
- msg.callback中的callback實際上是handler.post(runable r)中的r。handleCallback代碼:
private static void handleCallback(Message message) { message.callback.run(); }
從這可以看出,handleCallback實際操作是調用r.run()方法,即調用post過來的線程的run方法執行該線程的邏輯。
2. mCallback :當我們建立一個handler對象時,handler有一個構造方法:
public Handler(Callback callback){ this(callback, false); }
其中Callback是一個介面,即:
public interface Callback { public boolean handleMessage(Message msg); }
mCallback.handleMessage(msg)實際調用的就是構造handler時傳入的callback對象的handlermessage方法。
3. 最後調用的 handleMessage(msg)方法實際上是我們自己構造一個handler時 @Override 的handleMessage(Message msg)方法。
需要特別指明的是:
handler.post(runnable r)實際執行在handler所在的線程中。
官方說明為:
*Causes the Runnable r to be added to the message queue.
* The runnable will be run on the thread to which this handler is
* attached. *
在view中也有類似的post(runnable r)方法,但該方法執行在UI線程中。
官方說明為:
Causes the Runnable to be added to the message queue.The runnable will be run on the user interface thread.
Looper
在一個線程中,只有一個Looper對象。Looper在實現上使用的是單例模式。
Looper.prepare()代碼:
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)); }
new Looper(quitAllowed)代碼:
private Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); mThread = Thread.currentThread();//當前looper所線上程 }
在這裡可以看出,looper的構造方法是私人的,表明當前Looper使用的是單例模式,只有在myLooper()方法中才返回,代碼 :return sThreadLocal.get()。
總結
Handler:發送和處理message
Looper:為一個線程建立以messagequeue,使得在該線程中可以使用Handler。
Android訊息傳遞機制