Android 消息機制 | 藝術(shù)探索筆記

Android 消息機制主要指 Handler 的運行機制战授,包括了 MessageQueue掰读、Looper 和 Handler 的共同作用。其中 MessageQueue 以隊列的形式對外提供插入和刪除,它內(nèi)部由單鏈表實現(xiàn)裁赠。Looper 的作用是處理 MessageQueue 中存儲的消息。

通常赴精,Handler 被我們用來更新 UI贮喧,有如下兩種常見的用法

public class MainActivity extends AppCompactActivity implements View.OnClickListener {

    ...

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            // 進(jìn)行 UI 操作
        }
    };

    ...

    @Override
    public void onClick(View v) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                ...

                handler.sendMessage(message);
            }
        }).start();
    }
}

或者

public class MainActivity extends AppCompactActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Handler handler = new Handler();
        new Thread(new Runnable() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        // 進(jìn)行 UI 操作
                    }
                });
            }
        }).start();
    }
}

用法一

在子線程完成耗時操作后巍膘,通過 sendMessage 方法發(fā)送消息,回到 Handler 所在的主線程,通過 handleMessage 方法完成 UI 操作许饿。

來看 sendMessage 和它的后續(xù)方法

public final boolean sendMessage(Message msg) {
    return sendMessageDelayed(msg, 0);
}

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);
}

可以看到,經(jīng)過這幾個方法宽档,發(fā)送的消息最終都傳入了 MessageQueue 的 enqueueMessage 方法中图谷,于是我們找到 enqueueMessage 方法

boolean enqueueMessage(Message msg, long when) {

    ...

    synchronized (this) {

        ...

        msg.markInUse();
        msg.when = when;
        Message p = mMessages;
        boolean needWake;
        if (p == null || when == 0 || when < p.when) {
            // New head, wake up the event queue if blocked.
            msg.next = p;
            mMessages = msg;
            needWake = mBlocked;
        } else {
            // Inserted within the middle of the queue.  Usually we don't have to wake
            // up the event queue unless there is a barrier at the head of the queue
            // and the message is the earliest asynchronous message in the queue.
            needWake = mBlocked && p.target == null && msg.isAsynchronous();
            Message prev;
            for (;;) {
                prev = p;
                p = p.next;
                if (p == null || when < p.when) {
                    break;
                }
                if (needWake && p.isAsynchronous()) {
                    needWake = false;
                }
            }
            msg.next = p; // invariant: p == prev.next
            prev.next = msg;
        }

        // We can assume mPtr != 0 because mQuitting is false.
        if (needWake) {
            nativeWake(mPtr);
        }
    }
    return true;
}

在 enqueueMessage 方法中,它主要執(zhí)行的是單鏈表的插入操作逐哈,并沒有對消息隊列里的消息執(zhí)行芬迄,那執(zhí)行操作在哪呢?開頭說到 Looper 的作用是執(zhí)行 MessageQueue 中的消息昂秃,于是找到 Looper 的 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();
    }
}

可以看到禀梳,loop 方法是一個死循環(huán)杜窄,它會調(diào)用 MessageQueue 的 next 方法來獲取新消息,當(dāng) next 方法返回空時會退出循環(huán)算途。來看 next 方法源碼

Message next() {

    ...

    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;
            }

            ...

        }

        ...

    }
}

next 方法用 mMessage 管理消息隊列塞耕。當(dāng)消息隊列不為空時,返回并刪除當(dāng)前消息嘴瓤,并將下一條消息置前扫外。若消息隊列為空,next 方法會一直阻塞廓脆,直到有新消息到來筛谚。那 next 方法何時返回空呢?當(dāng) Looper 的 quit 方法調(diào)用時狞贱,next 會返回空刻获。因為當(dāng) Looper 調(diào)用 quit 方法,MessageQueue 的 quit 或 quitSafely 方法會被調(diào)用瞎嬉,此時消息隊列會被標(biāo)記為退出狀態(tài)蝎毡,next 判斷到消息隊列的狀態(tài)便會返回空。

接著看 loop 方法氧枣,如果 next 方法返回了消息沐兵,它會調(diào)用msg.target.dispatchMessage(msg)來處理,其中msg.target就是發(fā)送消息的 Handler 對象便监。也就是說通過 sendMessage 方法發(fā)送的消息會來到 Handler 對象的 dispatchMessage 方法扎谎,來看 dispatchMessage 方法

public void dispatchMessage(Message msg) {
    if (msg.callback != null) {
        handleCallback(msg);
    } else {
        if (mCallback != null) {
            if (mCallback.handleMessage(msg)) {
                return;
            }
        }
        handleMessage(msg);
    }
}

dispatchMessage 方法會檢查 Message 的 callback 是否為空,不為空就通過 handleCallback 方法來處理消息烧董。Message 的 callback 是一個 Runnable 對象毁靶,它實際上就是 post 方法傳來的 Runnable 參數(shù)。在 handleCallback 方法中會執(zhí)行傳來的 Runnable 對象

private static void handleCallback(Message message) {
    message.callback.run();
}

接著 dispatchMessage 方法會判斷 mCallback 是否為空逊移,若不為空预吆,調(diào)用 mCallback 的 handleMessage 方法,若為空胳泉,則調(diào)用 Handler 的 handleMessage 方法拐叉。其中 mCallback 的 handleMessage 方法指的是通過 Callback 接口實現(xiàn)的 handleMessage 方法

/**
 * Callback interface you can use when instantiating a Handler to avoid
 * having to implement your own subclass of Handler.
 *
 * @param msg A {@link android.os.Message Message} object
 * @return True if no further handling is desired
 */
public interface Callback {
    public boolean handleMessage(Message msg);
}

就這樣,通過 sendMessage 發(fā)送的消息最終回到了 handleMessage 方法進(jìn)行處理扇商。

用法二

在子線程中通過 post 方法傳入一個 Runnable 對象凤瘦,在該對象中實現(xiàn) UI 操作。

來看 post 方法

public final boolean post(Runnable r) {
    return  sendMessageDelayed(getPostMessage(r), 0);
}

這里使用了 sendMessageDelayed 方法發(fā)送了一條消息案铺。getPostMessage 方法的作用是將 Runnable 對象轉(zhuǎn)換成一條消息

private static Message getPostMessage(Runnable r) {
    Message m = Message.obtain();
    m.callback = r;
    return m;
}

sendMessageDelayed 方法經(jīng)過的流程在上面已經(jīng)分析過蔬芥。最終,通過 post 傳入的 Runnable 對象會來到 dispatchMessage 方法中,被 handleCallback 方法調(diào)用坝茎。

補充

在子線程中直接創(chuàng)建 Handler 會報錯涤姊,正確的用法是

class LooperThread extends Thread {
    
    public Handler mHandler;

    public void run() {
      Looper.prepare();

      mHandler = new Handler() {
          public void handleMessage(Message msg) {
              // process incoming messages here
          }
      };

      Looper.loop();
    }
}

在主線程中不必這樣,因為它會調(diào)用Looper.prepareMainLooper方法來創(chuàng)建 Looper嗤放。

相關(guān)參考 郭霖的博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市壁酬,隨后出現(xiàn)的幾起案子次酌,更是在濱河造成了極大的恐慌,老刑警劉巖舆乔,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件岳服,死亡現(xiàn)場離奇詭異,居然都是意外死亡希俩,警方通過查閱死者的電腦和手機吊宋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來颜武,“玉大人璃搜,你說我怎么就攤上這事×凵希” “怎么了这吻?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長篙议。 經(jīng)常有香客問我唾糯,道長,這世上最難降的妖魔是什么鬼贱? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任移怯,我火速辦了婚禮,結(jié)果婚禮上这难,老公的妹妹穿的比我還像新娘舟误。我一直安慰自己,他們只是感情好雁佳,可當(dāng)我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布脐帝。 她就那樣靜靜地躺著,像睡著了一般糖权。 火紅的嫁衣襯著肌膚如雪堵腹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天星澳,我揣著相機與錄音疚顷,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛腿堤,可吹牛的內(nèi)容都是我干的阀坏。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼笆檀,長吁一口氣:“原來是場噩夢啊……” “哼忌堂!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起酗洒,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤士修,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后樱衷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體棋嘲,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年矩桂,在試婚紗的時候發(fā)現(xiàn)自己被綠了沸移。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡侄榴,死狀恐怖雹锣,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情牲蜀,我是刑警寧澤笆制,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站涣达,受9級特大地震影響在辆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜度苔,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一匆篓、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧寇窑,春花似錦鸦概、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至饮笛,卻和暖如春咨察,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背福青。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工摄狱, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留脓诡,地道東北人。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓媒役,卻偏偏與公主長得像祝谚,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子酣衷,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容