日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

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

Android沒(méi)有全局的消息隊(duì)列,Android的消息隊(duì)列是和某個(gè)線程相關(guān)聯(lián)在一起的。每個(gè)線程最多只有一個(gè)消息隊(duì)列,消息的處理也是在這個(gè)線程中完成。也就是說(shuō),如果想在當(dāng)前線程中使用消息模型,則必須構(gòu)建一個(gè)消息隊(duì)列,消息機(jī)制的主要類(lèi)是: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è)無(wú)限循環(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。主要存放所有通過(guò)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來(lái)攜帶一些整型數(shù)據(jù)、obj攜帶Object對(duì)象,用于在不同線程間交換數(shù)據(jù)。

異步消息處理的整個(gè)流程:
首先需要在主線程中創(chuàng)建一個(gè)Handler對(duì)象,并重寫(xiě)handleMessage()方法; 然后,當(dāng)子線程中需要UI操作時(shí),就創(chuàng)建一個(gè)Message對(duì)象,并通過(guò)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ù)量)的軍營(yíng)(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則取出,沒(méi)有則創(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,來(lái)此Activity大本營(yíng),是你這次任務(wù)的接收者,一會(huì)我會(huì)帶你去首都的消息中心去報(bào)道。"

Handler:
來(lái)此Activity大本營(yíng)的Handler部門(mén)是整個(gè)消息機(jī)制的核心部門(mén),部門(mén)里有很多個(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都屬于重寫(xiě)了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;
????}


通過(guò)Looper.myLooper()方法獲得當(dāng)前線程保存的Looper實(shí)例,通過(guò)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說(shuō):我已經(jīng)跟首都的消息中心打好了招呼,準(zhǔn)備接收你了,現(xiàn)在有兩種車(chē)“send”和“post”你想坐哪輛都可以,不過(guò)要根據(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類(lèi)在發(fā)送消息,除了sendMessageAtFrontOfQueue()外,其余send方法都經(jīng)過(guò)層層包裝走到sendMessageAtTime()中。 這時(shí)小mMessage和mHandler上了sendMessage的車(chē),行駛在一條叫enqueueMessage的高速公路上進(jìn)入MessageQueue。將Message按時(shí)間排序,放入MessageQueue中。其中mMessage.target = this,是保證每個(gè)發(fā)送Message的Handler也能處理這個(gè)Message。mHandler向小mMessage說(shuō),其實(shí)你的消息到時(shí)候也是我處理的,不過(guò)現(xiàn)在還不是時(shí)候,因?yàn)槲液苊Α?br />Looper
路上時(shí)間,mHandler為小mMessage熱心介紹著MessageQueue和Looper?!霸诿總€(gè)駐扎地(線程)中只有一個(gè)MessageQueue和一個(gè)Looper,他們兩個(gè)是相愛(ài)相殺,同生共死的好朋友,Looper是個(gè)跑不死的郵差,一直負(fù)責(zé)取出MessageQueue中的Message”。 "不過(guò)通常只有首都(主線程)的Looper和MessageQueue是創(chuàng)建好的,其他地方需要我們?nèi)藶閯?chuàng)建"。 Looper提供prepare()方法來(lái)創(chuàng)建Looper。重復(fù)創(chuàng)建會(huì)拋出異常,也就是說(shuō)每個(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來(lái)到了MessageQueue中,進(jìn)入隊(duì)列之前,門(mén)衛(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ì)他說(shuō):“喔,又是mHandler帶來(lái)的啊,那把你交給他處理了?!膘话驳男Message看到了一個(gè)熟悉的身影,mHandler,可能是接觸太多Message,為了讓mHandler想起自己,mMessage說(shuō)出了上司教他的暗號(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ù)傳遞過(guò)去。在handleMessage()方法中,小mMessage出色的完成了任務(wù)。

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

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

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

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

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

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

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

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

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

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

關(guān)鍵字: 電動(dòng)汽車(chē) 新能源 驅(qū)動(dòng)電源

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

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

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

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

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

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

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

關(guān)鍵字: LED 驅(qū)動(dòng)電源 開(kāi)關(guān)電源

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

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