www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁 > > 充電吧
[導(dǎo)讀]Android沒有全局的消息隊列,Android的消息隊列是和某個線程相關(guān)聯(lián)在一起的。每個線程最多只有一個消息隊列,消息的處理也是在這個線程中完成。也就是說,如果想在當(dāng)前線程中使用消息模型,則必須構(gòu)建

Android沒有全局的消息隊列,Android的消息隊列是和某個線程相關(guān)聯(lián)在一起的。每個線程最多只有一個消息隊列,消息的處理也是在這個線程中完成。也就是說,如果想在當(dāng)前線程中使用消息模型,則必須構(gòu)建一個消息隊列,消息機(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ā)出的消息最終會傳遞到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時時刻刻監(jiān)視著MessageQueue,是每個線程中的MessageQueue管家,每個線程中只有一個Looper,調(diào)用其loop()方法就會進(jìn)入到一個無限循環(huán)中,每當(dāng)發(fā)現(xiàn)MessageQueue中存在一條消息,就會把它取出,送到Handler中的handleMessage()中。 public final class 3、
MessageQueue extends Object
java.lang.Object ???? android.os.MessageQueue 消息隊列,每個線程中只會有一個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對象,用于在不同線程間交換數(shù)據(jù)。

異步消息處理的整個流程:
首先需要在主線程中創(chuàng)建一個Handler對象,并重寫handleMessage()方法; 然后,當(dāng)子線程中需要UI操作時,就創(chuàng)建一個Message對象,并通過Handler將消息發(fā)送出去; 之后這條消息會被添加到MessageQueue隊列中,等待被處理,期間Looper會一直嘗試從MessageQueue中取出待處理消息,最后分發(fā)到Handler的handleMessage()方法中。由于Handler是在主線程中創(chuàng)建的,因此handleMessage()中的代碼也會在主線程中處理。
MeloDev的Message游歷:
Message
在邊境X(子線程)服役的士兵Message慵懶的躺在一個人數(shù)為50(線程中最大數(shù)量)的軍營(Message池)中。不料這時突然接到上司的obtain()命令,讓它去首都(主線程)告訴中央領(lǐng)導(dǎo)一些神秘代碼。小mMessage慌亂地整理下衣角和帽子,帶上信封,準(zhǔn)備出發(fā)。上司讓士兵mMessage收拾完畢等待一個神秘人電話,并囑咐他:到了首都之后,0是這次的暗號。

Message?mMessage?=?Message.obtain();
Bundle?bundle?=?new?Bundle();
bundle.putString("key","這里一切安全");
mMessage.what?=?0;
mMessage.obj?=?bundle;

通常會用obtain()方法創(chuàng)建Message,如果消息池中有Message則取出,沒有則創(chuàng)建,這樣防止對象重復(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接到一個店換,"我叫Handler,來此Activity大本營,是你這次任務(wù)的接收者,一會我會帶你去首都的消息中心去報道。"

Handler:
來此Activity大本營的Handler部門是整個消息機(jī)制的核心部門,部門里有很多個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)建任何一個Handler都屬于重寫了Activity的Handler。

在Handler的構(gòu)造中,默認(rèn)完成了對當(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實例,通過Looper.mQueue()獲得MessageQueue實例, static Looper myLooper()Return the Looper object associated with the current thread. static MessageQueue myQueue()Return the MessageQueue object associated with the current thread. 在此時,mHandler實例與looper、messageQueue實例關(guān)聯(lián)上了。
mHandler神情驕傲的對小mMessage說:我已經(jīng)跟首都的消息中心打好了招呼,準(zhǔn)備接收你了,現(xiàn)在有兩種車“send”和“post”你想坐哪輛都可以,不過要根據(jù)你上司的命令選擇對應(yīng)的型號哦~
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()中。 這時小mMessage和mHandler上了sendMessage的車,行駛在一條叫enqueueMessage的高速公路上進(jìn)入MessageQueue。將Message按時間排序,放入MessageQueue中。其中mMessage.target = this,是保證每個發(fā)送Message的Handler也能處理這個Message。mHandler向小mMessage說,其實你的消息到時候也是我處理的,不過現(xiàn)在還不是時候,因為我很忙。
Looper
路上時間,mHandler為小mMessage熱心介紹著MessageQueue和Looper?!霸诿總€駐扎地(線程)中只有一個MessageQueue和一個Looper,他們兩個是相愛相殺,同生共死的好朋友,Looper是個跑不死的郵差,一直負(fù)責(zé)取出MessageQueue中的Message”。 "不過通常只有首都(主線程)的Looper和MessageQueue是創(chuàng)建好的,其他地方需要我們?nèi)藶閯?chuàng)建"。 Looper提供prepare()方法來創(chuàng)建Looper。重復(fù)創(chuàng)建會拋出異常,也就是說每個線程只能有一個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)建了和他一一對應(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)入隊列之前,門衛(wèi)仔細(xì)給小mMessage貼上以下標(biāo)簽:“mHandler負(fù)責(zé)帶入”、“處理時間為0ms”并告訴小mMessage一定要按時間順序排隊。進(jìn)入隊伍中,Looper正不辭辛勞的將一個個跟小mMessage一樣的士兵帶走。 public static void loop() Run the message queue in this thread. Be sure to callquit() to end the loop. ? loop()方法有一個for死循環(huán),不斷調(diào)用queue.next()方法,在消息隊列中取出Message。并在Message中取出target,這個target就是發(fā)送消息的mHandler調(diào)用它的dispatchMessage()方法。
首都的MessageQueue中心雖然message很多,但大家都按時間排著隊,輪到mMessage了,Looper看了小mMessage的標(biāo)簽,對他說:“喔,又是mHandler帶來的啊,那把你交給他處理了。”忐忑不安的小mMessage看到了一個熟悉的身影,mHandler,可能是接觸太多Message,為了讓mHandler想起自己,mMessage說出了上司教他的暗號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()方法,并將消息對象作為參數(shù)傳遞過去。在handleMessage()方法中,小mMessage出色的完成了任務(wù)。

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

LED驅(qū)動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: 驅(qū)動電源

在工業(yè)自動化蓬勃發(fā)展的當(dāng)下,工業(yè)電機(jī)作為核心動力設(shè)備,其驅(qū)動電源的性能直接關(guān)系到整個系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動勢抑制與過流保護(hù)是驅(qū)動電源設(shè)計中至關(guān)重要的兩個環(huán)節(jié),集成化方案的設(shè)計成為提升電機(jī)驅(qū)動性能的關(guān)鍵。

關(guān)鍵字: 工業(yè)電機(jī) 驅(qū)動電源

LED 驅(qū)動電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個照明設(shè)備的使用壽命。然而,在實際應(yīng)用中,LED 驅(qū)動電源易損壞的問題卻十分常見,不僅增加了維護(hù)成本,還影響了用戶體驗。要解決這一問題,需從設(shè)計、生...

關(guān)鍵字: 驅(qū)動電源 照明系統(tǒng) 散熱

根據(jù)LED驅(qū)動電源的公式,電感內(nèi)電流波動大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關(guān)鍵字: LED 設(shè)計 驅(qū)動電源

電動汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產(chǎn)業(yè)的重要發(fā)展方向。電動汽車的核心技術(shù)之一是電機(jī)驅(qū)動控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機(jī)驅(qū)動系統(tǒng)中的關(guān)鍵元件,其性能直接影響到電動汽車的動力性能和...

關(guān)鍵字: 電動汽車 新能源 驅(qū)動電源

在現(xiàn)代城市建設(shè)中,街道及停車場照明作為基礎(chǔ)設(shè)施的重要組成部分,其質(zhì)量和效率直接關(guān)系到城市的公共安全、居民生活質(zhì)量和能源利用效率。隨著科技的進(jìn)步,高亮度白光發(fā)光二極管(LED)因其獨(dú)特的優(yōu)勢逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關(guān)鍵字: 發(fā)光二極管 驅(qū)動電源 LED

LED通用照明設(shè)計工程師會遇到許多挑戰(zhàn),如功率密度、功率因數(shù)校正(PFC)、空間受限和可靠性等。

關(guān)鍵字: LED 驅(qū)動電源 功率因數(shù)校正

在LED照明技術(shù)日益普及的今天,LED驅(qū)動電源的電磁干擾(EMI)問題成為了一個不可忽視的挑戰(zhàn)。電磁干擾不僅會影響LED燈具的正常工作,還可能對周圍電子設(shè)備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來解決L...

關(guān)鍵字: LED照明技術(shù) 電磁干擾 驅(qū)動電源

開關(guān)電源具有效率高的特性,而且開關(guān)電源的變壓器體積比串聯(lián)穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機(jī)重量也有所下降,所以,現(xiàn)在的LED驅(qū)動電源

關(guān)鍵字: LED 驅(qū)動電源 開關(guān)電源

LED驅(qū)動電源是把電源供應(yīng)轉(zhuǎn)換為特定的電壓電流以驅(qū)動LED發(fā)光的電壓轉(zhuǎn)換器,通常情況下:LED驅(qū)動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: LED 隧道燈 驅(qū)動電源
關(guān)閉