Handler、Message的簡單使用
Android沒有全局的消息隊(duì)列,Android的消息隊(duì)列是和某個(gè)線程相關(guān)聯(lián)在一起的。每個(gè)線程最多只有一個(gè)消息隊(duì)列,消息的處理也是在這個(gè)線程中完成。也就是說,如果想在當(dāng)前線程中使用消息模型,則必須構(gòu)建一個(gè)消息隊(duì)列,消息機(jī)制的主要類是:Looper、Handler、MessageQueue、Message.
1、
public class
Handler
extends Object
java.lang.Object
????
android.os.Handler
Class Overview
A Handler allows you to send and process Message
and Runnable objects associated with a thread'sMessageQueue
. Each Handler instance is
associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue
and execute them as they come out of the message queue.
?
?mHandler?=?new?Handler(){ public?void?handleMessage(Message?msg){ //int?android.os.Message.what //User-defined?message?code?so?that?the?recipient?can?identify?what?this?message?is?about. switch(msg.what){ ?case?UPDATE_TEXT: mTextView.setText("text?changed"); ?break; default: ?break; } ????????????????????????????????} };
public final boolean sendMessage(Message msg)Added inAPI
level 1
Pushes a message onto the end of the message queue after all pending messages before the current time. It will be received inhandleMessage(Message)
, in the thread attached to this handler.
Returns Returns true if the message was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.
Message?mMessage?=?Message.obtain(mHandler,?UPDATE_TEXT); //Pushes?a?message?onto?the?end?of?the?message?queue?after?all?pending?messages?before?the?current?time.? mHandler.sendMessage(mMessage);
Handler負(fù)責(zé)將Message發(fā)送至當(dāng)前線程的MessageQueue中,處理消息。發(fā)送消息一般是使用Handler的sendMessage方法,發(fā)出的消息最終會(huì)傳遞到Handler的handleMessage()方法中。
public final class
2、
Looper
extends Object
java.lang.Object
????
android.os.Looper
Class Overview
Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, callprepare()
in the thread that is to run the loop, and thenloop()
to have it process messages until the loop is stopped.
Most interaction with a message loop is through the Handler
class.?
Looper時(shí)時(shí)刻刻監(jiān)視著MessageQueue,是每個(gè)線程中的MessageQueue管家,每個(gè)線程中只有一個(gè)Looper,調(diào)用其loop()方法就會(huì)進(jìn)入到一個(gè)無限循環(huán)中,每當(dāng)發(fā)現(xiàn)MessageQueue中存在一條消息,就會(huì)把它取出,送到Handler中的handleMessage()中。
public final class
3、
MessageQueue
extends Object
java.lang.Object
????
android.os.MessageQueue
消息隊(duì)列,每個(gè)線程中只會(huì)有一個(gè)MessageQueue。主要存放所有通過Handler發(fā)送的消息。
4、
public final class
Message
extends Object
implements Parcelable
java.lang.Object
????
android.os.Message
Class Overview
Defines a message containing a description and arbitrary data object that can be sent to aHandler
. This object contains two extra int fields and an extra object field that allow you to not do allocations
in many cases.?
//Message?android.os.Message.obtain(Handler?h,?int?what) Message?mMessage?=?Message.obtain(mHandler,?UPDATE_TEXT);
Message是在線程之間傳遞消息,它可以在內(nèi)部攜帶少量信息,如what字段、arg1、arg2來攜帶一些整型數(shù)據(jù)、obj攜帶Object對(duì)象,用于在不同線程間交換數(shù)據(jù)。
異步消息處理的整個(gè)流程:
首先需要在主線程中創(chuàng)建一個(gè)Handler對(duì)象,并重寫handleMessage()方法;
然后,當(dāng)子線程中需要UI操作時(shí),就創(chuàng)建一個(gè)Message對(duì)象,并通過Handler將消息發(fā)送出去;
之后這條消息會(huì)被添加到MessageQueue隊(duì)列中,等待被處理,期間Looper會(huì)一直嘗試從MessageQueue中取出待處理消息,最后分發(fā)到Handler的handleMessage()方法中。由于Handler是在主線程中創(chuàng)建的,因此handleMessage()中的代碼也會(huì)在主線程中處理。
MeloDev的Message游歷:
Message
在邊境X(子線程)服役的士兵Message慵懶的躺在一個(gè)人數(shù)為50(線程中最大數(shù)量)的軍營(Message池)中。不料這時(shí)突然接到上司的obtain()命令,讓它去首都(主線程)告訴中央領(lǐng)導(dǎo)一些神秘代碼。小mMessage慌亂地整理下衣角和帽子,帶上信封,準(zhǔn)備出發(fā)。上司讓士兵mMessage收拾完畢等待一個(gè)神秘人電話,并囑咐他:到了首都之后,0是這次的暗號(hào)。
Message?mMessage?=?Message.obtain(); Bundle?bundle?=?new?Bundle(); bundle.putString("key","這里一切安全"); mMessage.what?=?0; mMessage.obj?=?bundle;
通常會(huì)用obtain()方法創(chuàng)建Message,如果消息池中有Message則取出,沒有則創(chuàng)建,這樣防止對(duì)象重復(fù)創(chuàng)建,節(jié)省資源。 obtain()方法源碼:
?/** ?????*?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; ????????????????sPoolSize--; ????????????????return?m; ????????????} ????????} ????????return?new?Message(); ????}
“鈴鈴鈴……”,小mMessage接到一個(gè)店換,"我叫Handler,來此Activity大本營,是你這次任務(wù)的接收者,一會(huì)我會(huì)帶你去首都的消息中心去報(bào)道。"
Handler:
來此Activity大本營的Handler部門是整個(gè)消息機(jī)制的核心部門,部門里有很多個(gè)Handler,這次協(xié)助小mMessage的叫mHandler.
?mHandler?=?new?Handler(){ public?void?handleMessage(Message?msg){ //int?android.os.Message.what //User-defined?message?code?so?that?the?recipient?can?identify?what?this?message?is?about. } };
Handler屬于Activity,創(chuàng)建任何一個(gè)Handler都屬于重寫了Activity的Handler。
在Handler的構(gòu)造中,默認(rèn)完成了對(duì)當(dāng)前線程Looper的綁定。
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()方法獲得當(dāng)前線程保存的Looper實(shí)例,通過Looper.mQueue()獲得MessageQueue實(shí)例,
static Looper
myLooper()Return the Looper object associated with the current thread.
static MessageQueue
myQueue()Return the MessageQueue
object associated with the current thread.
在此時(shí),mHandler實(shí)例與looper、messageQueue實(shí)例關(guān)聯(lián)上了。
mHandler神情驕傲的對(duì)小mMessage說:我已經(jīng)跟首都的消息中心打好了招呼,準(zhǔn)備接收你了,現(xiàn)在有兩種車“send”和“post”你想坐哪輛都可以,不過要根據(jù)你上司的命令選擇對(duì)應(yīng)的型號(hào)哦~
post、send:
final boolean
post(Runnable r)Causes the Runnable r to be added to the message queue.
final boolean
postAtFrontOfQueue(Runnable r)Posts a message to an object that implements Runnable.
final boolean
postAtTime(Runnable r,Object token, long uptimeMillis)Causes the Runnable r to be added to the message queue, to be run at a specific time given by.
final boolean
postAtTime(Runnable r, long uptimeMillis)Causes the Runnable r to be added to the message queue, to be run at a specific time given by.
final boolean
postDelayed(Runnable r, long delayMillis)Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses
final boolean
sendEmptyMessage(int what)Sends a Message containing only the what value.
final boolean
sendEmptyMessageAtTime(int what, long uptimeMillis)Sends a Message containing only the what value, to be delivered at a specific time.
final boolean
sendEmptyMessageDelayed(int what, long delayMillis)Sends a Message containing only the what value, to be delivered after the specified amount of time elapses.
final boolean
sendMessage(Message msg)Pushes a message onto the end of the message queue after all pending messages before the current time.
final boolean
sendMessageAtFrontOfQueue(Message msg)Enqueue a message at the front of the message queue, to be processed on the next iteration of the message loop.
boolean
sendMessageAtTime(Message msg, long uptimeMillis)Enqueue a message into the message queue after all pending messages before the absolute time (in milliseconds).
final boolean
sendMessageDelayed(Message msg, long delayMillis)Enqueue a message into the message queue after all pending messages before (current time + delayMillis).
String
toString()
分析源碼,post方法也是在使用send類在發(fā)送消息,除了sendMessageAtFrontOfQueue()外,其余send方法都經(jīng)過層層包裝走到sendMessageAtTime()中。
這時(shí)小mMessage和mHandler上了sendMessage的車,行駛在一條叫enqueueMessage的高速公路上進(jìn)入MessageQueue。將Message按時(shí)間排序,放入MessageQueue中。其中mMessage.target = this,是保證每個(gè)發(fā)送Message的Handler也能處理這個(gè)Message。mHandler向小mMessage說,其實(shí)你的消息到時(shí)候也是我處理的,不過現(xiàn)在還不是時(shí)候,因?yàn)槲液苊Α?br />Looper
路上時(shí)間,mHandler為小mMessage熱心介紹著MessageQueue和Looper。“在每個(gè)駐扎地(線程)中只有一個(gè)MessageQueue和一個(gè)Looper,他們兩個(gè)是相愛相殺,同生共死的好朋友,Looper是個(gè)跑不死的郵差,一直負(fù)責(zé)取出MessageQueue中的Message”。
"不過通常只有首都(主線程)的Looper和MessageQueue是創(chuàng)建好的,其他地方需要我們?nèi)藶閯?chuàng)建"。
Looper提供prepare()方法來創(chuàng)建Looper。重復(fù)創(chuàng)建會(huì)拋出異常,也就是說每個(gè)線程只能有一個(gè)looper。
Looper.prepare();
static void
prepareMainLooper()Initialize the current thread as a looper, marking it as an application's main looper.
Looper的構(gòu)造方法中,創(chuàng)建了和他一一對(duì)應(yīng)的MessageQueue
private?Looper(boolean?quitAllowed){ ???mQueue?=?new?MessageQueue(quitAllowed); ???mThread?=?Thread.currentThread(); }
在Android中ActivityThread的main方法是程序入口,主線程的Looper和MessageQueue就是在此刻創(chuàng)建。
mHandler和小mMessage來到了MessageQueue中,進(jìn)入隊(duì)列之前,門衛(wèi)仔細(xì)給小mMessage貼上以下標(biāo)簽:“mHandler負(fù)責(zé)帶入”、“處理時(shí)間為0ms”并告訴小mMessage一定要按時(shí)間順序排隊(duì)。進(jìn)入隊(duì)伍中,Looper正不辭辛勞的將一個(gè)個(gè)跟小mMessage一樣的士兵帶走。
public static void loop()
Run the message queue in this thread. Be sure to callquit()
to end the loop. ?
loop()方法有一個(gè)for死循環(huán),不斷調(diào)用queue.next()方法,在消息隊(duì)列中取出Message。并在Message中取出target,這個(gè)target就是發(fā)送消息的mHandler調(diào)用它的dispatchMessage()方法。
首都的MessageQueue中心雖然message很多,但大家都按時(shí)間排著隊(duì),輪到mMessage了,Looper看了小mMessage的標(biāo)簽,對(duì)他說:“喔,又是mHandler帶來的啊,那把你交給他處理了?!膘话驳男Message看到了一個(gè)熟悉的身影,mHandler,可能是接觸太多Message,為了讓mHandler想起自己,mMessage說出了上司教他的暗號(hào)0。
public?void?dispatchMessage(Message?msg){ if(msg.callback?!=?null){ handleCallback.handleMessage(msg); }else{ if(mCallback?!=?null){ if(mCallback.handleMessage(msg)){ return;} } handleMessage(msg); } }
dispatchMessage()方法:若mCallback不為空,則調(diào)用mCallback的handleMessage();否則,直接調(diào)用Handler的handleMessage()方法,并將消息對(duì)象作為參數(shù)傳遞過去。在handleMessage()方法中,小mMessage出色的完成了任務(wù)。