Handler-Looper-Message是Android中重要的異步消息處理機(jī)制捶牢,也是Android開發(fā)中不得不談的東西這也是Activity等組件中工作機(jī)制實(shí)現(xiàn)的重要部分秋麸,作為Android源碼閱讀在合適不過了。
1驯耻、概論
Message用于存儲(chǔ)數(shù)據(jù)可缚,Looper與當(dāng)前線程綁定票腰,創(chuàng)建一個(gè)MessageQueue,無限循環(huán)從中讀取Message,當(dāng)MessageQueue為空時(shí)阻塞测柠,Handler可以獲取線程綁定的Lopper,Handler發(fā)送Messager到Looper的MessageQueue,Looper取出的Message再交給Handler處理轰胁。(例:Handler用于子線程更新UI,主線程中創(chuàng)建Handler,它與主線程的Looper綁定,然后在子線程中使用Handler發(fā)送消息朝扼,Looper取出后Handler在它創(chuàng)建的主線程中執(zhí)行)
2擎颖、源碼解析
1、Message
Messsag用于存儲(chǔ)傳遞的數(shù)據(jù)驮俗。
/**
* 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是低成本數(shù)據(jù)的選擇方案
*如果只要存儲(chǔ)一些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;
創(chuàng)建Message不必使用它的構(gòu)造方法索烹,使用obtain();可以從Message池中取出一個(gè)Message,避免多次創(chuàng)建新的Message對(duì)象弱睦。
? ? * 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定義有一個(gè)MessageQueue對(duì)象和一個(gè)ThreadLocal對(duì)象,Looper只會(huì)與一個(gè)線程綁定子库,下面看源碼仑嗅,主要是prepare()和loop()兩個(gè)方法,Activity的啟動(dòng)代碼中鸵贬,在當(dāng)前UI線程就會(huì)調(diào)用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是一個(gè)ThreadLocal對(duì)象地沮,可以在一個(gè)線程中存儲(chǔ)變量摩疑。可以看到吉殃,將一個(gè)Looper的實(shí)例放入了ThreadLocal楷怒,并且2-4行判斷了sThreadLocal是否為null,否則拋出異常迫卢。這也就說明了Looper.prepare()方法不能被調(diào)用兩次,同時(shí)也保證了一個(gè)線程中只有一個(gè)Looper實(shí)例,接下來是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存儲(chǔ)的Looper實(shí)例,如果me為null則拋出異常眨层,這就是說looper方法必須在prepare方法之后才運(yùn)行。然后拿到該looper實(shí)例中的mQueue馒闷,就進(jìn)入了無限循環(huán)。然后一直取出一條消息逛薇,直到?jīng)]有消息則阻塞疏虫。
獲取消息后使用msg.target.dispatchMessage(msg);把消息交給msg的target的dispatchMessage方法去處理志电。Msg的target是什么呢墩莫?其實(shí)就是handler對(duì)象羞福,下面會(huì)進(jìn)行分析治专。最后釋放消息占據(jù)的資源恋昼。
3、Handle重要上場(chǎng)了
首先看下它是怎么與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;
? ?}
英文文檔寫的很詳細(xì),大意就是通過Looper.myLooper()獲取了當(dāng)前線程保存的Looper實(shí)例谤祖,然后又獲取了這個(gè)Looper實(shí)例中保存的MessageQueue(消息隊(duì)列)老速,這樣就保證了handler的實(shí)例與我們Looper實(shí)例中MessageQueue關(guān)聯(lián)上了。
接下來看下常用的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);
? ?}
都調(diào)用到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了,所以最終會(huì)調(diào)用queue的enqueueMessage的方法旁舰,也就是說handler發(fā)出的消息,最終會(huì)保存到消息隊(duì)列中去毯焕。然后就聯(lián)系上面Looper中的無限循環(huán)取出Message了磺樱,所以最終會(huì)調(diào)用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) {
? ?}
這個(gè)就是個(gè)空方法,所以需要我們對(duì)它進(jìn)行復(fù)寫芜辕。于是乎,處理Message就交到我們手中了乖仇。
3询兴、總結(jié)
1、首先利用Looper.prepare()在本線程綁定一個(gè)Looper實(shí)例警儒,然后該實(shí)例中保存一個(gè)MessageQueue對(duì)象蜀铲;因?yàn)長(zhǎng)ooper.prepare()在一個(gè)線程中只能調(diào)用一次属百,所以在一個(gè)線程中只有一個(gè)MessgeQueue。
2族扰、Looper.loop()會(huì)讓當(dāng)前線程進(jìn)入一個(gè)無限循環(huán)渔呵,不斷從MessageQueue的實(shí)例中讀取消息,然后回調(diào)msg.target.dispatchMessage(msg)方法耕驰。
3录豺、Handler的構(gòu)造方法,會(huì)首先得到當(dāng)前線程中保存的Looper實(shí)例媒抠,進(jìn)而與Looper實(shí)例中的MessageQueue關(guān)聯(lián)兢哭。
4迟螺、Handler的sendMessage方法,會(huì)給msg的target賦值為handler矩父,然后加入MessageQueue中。
5民轴、在構(gòu)造Handler實(shí)例時(shí)球订,我們會(huì)重寫handleMessage方法,也就是msg.target.dispatchMessage(msg)最終調(diào)用的方法微驶。