Loop,Message,MessageQueue概念理解:
Message:Handler發送、接收和處理的消息對象
Looper:每個線程只能擁有一個Looper.它的looper()方法負責循環讀取MessageQueue中的消息并將讀取到的消息交給發送該消息的handler進行處理。
MessageQueue:消息隊列,它采用先進先出的方式來管理Message。程序在創建Looper對象時,會在它的構造器中創建MessageQueue
Looper提供的源碼如下:
~~~
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
~~~
從源碼第2行中可以看出,在創建Looper對象時會創建一個與之關聯的MessageQueue對象。構造器是private修飾的,所以程序員是無法創建Looper對象的,就是說**創建Looper同時就創建了MessageQueue對象**
Handler:前面說Handler作用有兩個---發送消息和處理消息,Handler發送的消息必須被送到指定的MessageQueue,也就是說,要想Handler正常工作必須在當前線程中有一個MessageQueue,否則消息沒法保存。**而MessageQueue是由Looper負責管理的**,因此要想Handler正常工作,**必須在當前線程中有一個Looper對象**,這里分為兩種情況:
1>主線程(UI線程),系統已經初始化了一個Looper對象,因此程序直接創建Handler即可
2>程序員自己創建的子線程,這時,程序員必須創建一個Looper對象,并啟動它。
創建Looper:使用Looper.prepare(),查看源碼
~~~
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));
}
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
~~~
通過方法調用,第9行創建Looper對象,創建Looper對象時同時會創建MessageQueue對象(第13行)。此外,可以看出prepare()允許一個線程最多有一個Looper被創建
啟動Looper:Looper.loop(),loop()使用一個死循環不斷取出MessageQueue中的消息,并將消息發送給對應的Handler進行處理。下面是Looper類中looper()方法的部分源碼
~~~
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();
}
~~~
很明顯第1行用了一個死循環,第2行從queue中取出Message,第15行通過dispatchMessage(Message msg)方法將消息發送給Handler。
Looper,MessageQueue,Handler的各自作用如下:
Looper:每個線程只有一個Looper,他負責管理MessageQueue,會不斷的從MessageQueue中取出消息,將消息交給對應的Handler處理
MessageQueue:由Looper負責管理,是用來存放線程放入的消息。
Handler:它把消息發送給Looper管理的MessageQueue,并負責處理Looper分給它的消息
在線程中Handler的使用步驟是:
(1)調用 Looper的prepare()方法為當前線程創建Looper對象,創建Looper對象時,它的構造器會創建與之配套的MessageQueue。
(2)有了Looper之后,創建Handler子類的實例,重寫HandlerMessage()方法,該方法負責處理來自其它線程的消息。
(3)調用Looper的loop()方法啟動Looper。
- 前言
- Eclipse搭建android環境及Genymotion模擬器安裝問題解決方法
- 表格布局(TableLayout)及重要屬性
- 幀布局(FrameLayout)及屬性
- layout_width和width,layout_height和height
- UI組件之TextView及其子類
- UI組件之TextView及其子類(一)TextView和EditText
- UI組件之TextView及其子類(二)RadioButton和CheckBox
- UI組件之TextView及其子類(三)ToggleButton和Switch
- UI組件之TextView及其子類(四)AnalogClock,DigitalClock
- UI組件之TextView及其子類(五)計時器Chronometer
- UI組件之ImageView及其子類(一)ImageView顯示圖片
- UI組件之ImageView及其子類(二)ImageButton ,ZoomButton
- UI組件之AdapterView及其子類關系,Adapter接口及其實現類關系
- UI組件之AdapterView及其子類(一)三種Adapter適配器填充ListView
- UI組件之AdapterView及其子類(二)GridView網格視圖的使用
- UI組件之AdapterView及其子類(三)Spinner控件詳解
- UI組件之AdapterView及其子類(四)Gallery畫廊控件使用
- UI組件之AdapterView及其子類(五)ListView組件和ListActivity
- UI組件之AdapterView及其子類(六)ExpandableListView組件和ExpandableListActivity的使用
- UI組件之 ProgressBar及其子類(一)ProgressBar進度條的使用
- UI組件之ProgressBar及其子類(二)SeekBar拖動條和RatingBar星級評分條的使用
- ViewFlipper的功能和用法
- Toast的功能和用法
- TabHost選項卡的 功能和用法
- AlertDialog創建6種對話框的用法
- Android基于監聽的事件處理機制
- Android基于回調的事件處理
- Handler消息傳遞機制(一)
- Handler消息傳遞機制(二)Handler,Loop,Message,MessageQueue的工作原理
- 啟動Activity的兩種方式startActivity和startActivityForResult(一)
- 啟動Activity的兩種方式startActivity和startActivityForResult(二)
- Activity的生命周期理解
- Bundle在Activity之間交換數據
- 通過 Intent 傳遞類對象
- Intent對象詳解(一)
- Intent對象詳解(二)
- 使用指定的Action,Category調用系統Activity
- 使用Action,Data屬性啟動系統Activity
- Android數據存儲的三種方式-SharedPrefrences,File,SQLite