Handler-Looper-Message源碼解析

Handler-Looper-Message是Android中重要的異步消息處理機制,也是Android開發中不得不談的東西這也是Activity等組件中工作機制實現的重要部分,作為Android源碼閱讀在合適不過了。

1、概論

Message用于存儲數據,Looper與當前線程綁定,創建一個MessageQueue,無限循環從中讀取Message,當MessageQueue為空時阻塞,Handler可以獲取線程綁定的Lopper,Handler發送Messager到Looper的MessageQueue,Looper取出的Message再交給Handler處理。(例:Handler用于子線程更新UI,主線程中創建Handler,它與主線程的Looper綁定,然后在子線程中使用Handler發送消息,Looper取出后Handler在它創建的主線程中執行)

2、源碼解析

1、Message

Messsag用于存儲傳遞的數據。

/**
* arg1 and arg2 are lower-cost alternatives to using
* {@link #setData(Bundle) setData()} if *you only need to store a few integer *values.
*arg1和arg2是低成本數據的選擇方案
*如果只要存儲一些integer只用選擇它*們,不必使用setData();
? ? */
? ?public int arg1;
? ?/**
? ? * arg1 and arg2 are lower-cost alternatives to using
? ? * {@link #setData(Bundle) setData()} if you only need to store a
? ? * few integer values.
? ? */
? ?public int arg2;

創建Message不必使用它的構造方法,使用obtain();可以從Message池中取出一個Message,避免多次創建新的Message對象。

? ? * Return a new Message instance from the global pool. Allows us to
? ? * avoid allocating new objects in many cases.
? ? */
? ?public static Message obtain() {
? ? ? ?synchronized (sPoolSync) {
? ? ? ? ? ?if (sPool != null) {
? ? ? ? ? ? ? ?Message m = sPool;
? ? ? ? ? ? ? ?sPool = m.next;
? ? ? ? ? ? ? ?m.next = null;
? ? ? ? ? ? ? ?m.flags = 0; // clear in-use flag
? ? ? ? ? ? ? ?sPoolSize--;
? ? ? ? ? ? ? ?return m;
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?return new Message();
? ?}
? ?/**
? ? * Same as {@link #obtain()}, but copies the values of an existing
? ? * message (including its target) into the new one.
? ? * @param orig Original message to copy.
? ? * @return A Message object from the global pool.
? ? */
? ?public static Message obtain(Message orig) {
? ? ? ?Message m = obtain();
? ? ? ?m.what = orig.what;
? ? ? ?m.arg1 = orig.arg1;
? ? ? ?m.arg2 = orig.arg2;
? ? ? ?m.obj = orig.obj;
? ? ? ?m.replyTo = orig.replyTo;
? ? ? ?m.sendingUid = orig.sendingUid;
? ? ? ?if (orig.data != null) {
? ? ? ? ? ?m.data = new Bundle(orig.data);
? ? ? ?}
? ? ? ?m.target = orig.target;
? ? ? ?m.callback = orig.callback;
? ? ? ?return m;
? ?}
? ?/**
? ? * Same as {@link #obtain()}, but sets the value for the target member on the Message returned.
? ? * @param h ?Handler to assign to the returned Message object's target member.
? ? * @return A Message object from the global pool.
? ? */
? ?public static Message obtain(Handler h) {
? ? ? ?Message m = obtain();
? ? ? ?m.target = h;
? ? ? ?return m;
? ?}

2、Looper

Looper定義有一個MessageQueue對象和一個ThreadLocal對象,Looper只會與一個線程綁定,下面看源碼,主要是prepare()和loop()兩個方法,Activity的啟動代碼中,在當前UI線程就會調用Looper.prepare()和Looper.loop()方法。

? ? /** Initialize the current thread as a looper.
? ? ?* This gives you a chance to create handlers that then reference
? ? ?* this looper, before actually starting the loop. Be sure to call
? ? ?* {@link #loop()} after calling this method, and end it by calling
? ? ?* {@link #quit()}.
? ? ?*/
? ?public static void prepare() {
? ? ? ?prepare(true);
? ?}
? ?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));
? ?}

hreadLocal是一個ThreadLocal對象,可以在一個線程中存儲變量。可以看到,將一個Looper的實例放入了ThreadLocal,并且2-4行判斷了sThreadLocal是否為null,否則拋出異常。這也就說明了Looper.prepare()方法不能被調用兩次,同時也保證了一個線程中只有一個Looper實例,接下來是loop()方法

? ?/**
? ? * Run the message queue in this thread. Be sure to call
? ? * {@link #quit()} to end the loop.
? ? */
? ?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();
? ? ? ?}
? ?}
? ?/**
? ? * Return the Looper object associated with the current thread. ?Returns
? ? * null if the calling thread is not associated with a Looper.
? ? */
? ?public static Looper myLooper() {
? ? ? ?return sThreadLocal.get();
? ?}

方法中直接獲取了sThreadLocal存儲的Looper實例,如果me為null則拋出異常,這就是說looper方法必須在prepare方法之后才運行。然后拿到該looper實例中的mQueue,就進入了無限循環。然后一直取出一條消息,直到沒有消息則阻塞。

獲取消息后使用msg.target.dispatchMessage(msg);把消息交給msg的target的dispatchMessage方法去處理。Msg的target是什么呢?其實就是handler對象,下面會進行分析。最后釋放消息占據的資源。

3、Handle重要上場了

首先看下它是怎么與Looper鏈接起來的

? ?/**
? ? * Use the {@link Looper} for the current thread with the specified callback interface
? ? * and set whether the handler should be asynchronous.
? ? *
? ? * Handlers are synchronous by default unless this constructor is used to make
? ? * one that is strictly asynchronous.
? ? *
? ? * Asynchronous messages represent interrupts or events that do not require global ordering
? ? * with respect to synchronous messages. ?Asynchronous messages are not subject to
? ? * the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}.
? ? *
? ? * @param callback The callback interface in which to handle messages, or null.
? ? * @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for
? ? * each {@link Message} that is sent to it or {@link Runnable} that is posted to it.
? ? *
? ? * @hide
? ? */
? ?public Handler(Callback callback, boolean async) {
? ? ? ?if (FIND_POTENTIAL_LEAKS) {
? ? ? ? ? ?final Class klass = getClass();
? ? ? ? ? ?if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
? ? ? ? ? ? ? ? ? ?(klass.getModifiers() & Modifier.STATIC) == 0) {
? ? ? ? ? ? ? ?Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
? ? ? ? ? ? ? ? ? ?klass.getCanonicalName());
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?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;
? ?}

英文文檔寫的很詳細,大意就是通過Looper.myLooper()獲取了當前線程保存的Looper實例,然后又獲取了這個Looper實例中保存的MessageQueue(消息隊列),這樣就保證了handler的實例與我們Looper實例中MessageQueue關聯上了。

接下來看下常用的sendMessage()

public final boolean sendMessage(Message msg){
return sendMessageDelayed(msg, 0);
? ?}
public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
? ? ? ?Message msg = Message.obtain();
? ? ? ?msg.what = what;
? ? ? ?return sendMessageDelayed(msg, delayMillis);
? ?}

都調用到sendMessageDelayed

public final boolean sendMessageDelayed(Message msg, long delayMillis)
? ?{
? ? ? ?if (delayMillis < 0) {
? ? ? ? ? ?delayMillis = 0;
? ? ? ?}
? ? ? ?return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
? ?}
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);}
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
? ? ? ?msg.target = this;
? ? ? ?if (mAsynchronous) {
? ? ? ? ? ?msg.setAsynchronous(true);
? ? ? ?}
? ? ? ?return queue.enqueueMessage(msg, uptimeMillis);
? ?}

這里就給target賦值為this,就是上面說的target就是Handler了,所以最終會調用queue的enqueueMessage的方法,也就是說handler發出的消息,最終會保存到消息隊列中去。然后就聯系上面Looper中的無限循環取出Message了,所以最終會調用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);
? ? ? ?}
? ?}
/**
? ? * Subclasses must implement this to receive messages.
? ? */
? ?public void handleMessage(Message msg) {
? ?}

這個就是個空方法,所以需要我們對它進行復寫。于是乎,處理Message就交到我們手中了。

3、總結

1、首先利用Looper.prepare()在本線程綁定一個Looper實例,然后該實例中保存一個MessageQueue對象;因為Looper.prepare()在一個線程中只能調用一次,所以在一個線程中只有一個MessgeQueue。

2、Looper.loop()會讓當前線程進入一個無限循環,不斷從MessageQueue的實例中讀取消息,然后回調msg.target.dispatchMessage(msg)方法。

3、Handler的構造方法,會首先得到當前線程中保存的Looper實例,進而與Looper實例中的MessageQueue關聯。

4、Handler的sendMessage方法,會給msg的target賦值為handler,然后加入MessageQueue中。

5、在構造Handler實例時,我們會重寫handleMessage方法,也就是msg.target.dispatchMessage(msg)最終調用的方法。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容