/*
* Set this flag to true to detect anonymous, local or member classes
* that extend this Handler class and that are not static. These kind
* of classes can potentially create leaks.
* 將此標志設置為true以檢測此 Handler 類不是靜態(tài)的匿名,本地或成員類匪凉。這些類
* 可能會產(chǎn)生泄漏。
*/
private static final boolean FIND_POTENTIAL_LEAKS = false;
/**
* Callback interface you can use when instantiating a Handler to avoid
* having to implement your own subclass of Handler.
* 你可以在你實例化一個 Handler 的時候使用 Callback 接口(就是我們說的回調(diào)接口)
* 避免必須實現(xiàn)一個 Handler 的子類捺檬。
*
* @param msg A {@link android.os.Message Message} object
* @return True if no further handling is desired
*/
public interface Callback {
public boolean handleMessage(Message msg);
}
/**
* Subclasses must implement this to receive messages.
* 子類必須實現(xiàn)這個方法來接受 Message 對象
*/
public void handleMessage(Message msg) {
}
/**
* Handle system messages here.
* 該方法用于處理系統(tǒng)的 Message
*/
public void dispatchMessage(Message msg) {
// 如果傳入的 Message 的回調(diào)函數(shù)不為空
if (msg.callback != null) {
// 調(diào)用 handleCallback 方法
handleCallback(msg);
} else { // 不為空
if (mCallback != null) { // 如果 Handler 的 Callback 不為空
if (mCallback.handleMessage(msg)) { // 返回值為 true 調(diào)用 return
return;
}
}
handleMessage(msg);// 調(diào)用 handleMessage() 上面有介紹
}
}
/**
* Default constructor associates this handler with the {@link Looper} for the
* current thread.
*
* If this thread does not have a looper, this handler won't be able to receive messages
* so an exception is thrown.
* 默認的構(gòu)造函數(shù)再层,通過當前線程的 Lopper 對象關(guān)聯(lián)當前 Handler。
* 如果這個線程沒有 Lopper堡纬,那么該 Handler 將無法接受 Message聂受,因此會拋出一個異常。
*/
public Handler() {
this(null, false);
}
/**
* Constructor associates this handler with the {@link Looper} for the
* current thread and takes a callback interface in which you can handle
* messages.
*
* If this thread does not have a looper, this handler won't be able to receive messages
* so an exception is thrown.
*
* @param callback The callback interface in which to handle messages, or null.
* 處理 Message 的回調(diào)接口烤镐,或者傳 null
* 相對于默認構(gòu)造函數(shù)蛋济,添加了一個 Callback 參數(shù),表示會通過該 Callback 對象處理 Message
*/
public Handler(Callback callback) {
this(callback, false);
}
/**
* Use the provided {@link Looper} instead of the default one.
*
* @param looper The looper, must not be null.
* Looper 對象炮叶,不能傳入 null
* 使用傳入的 Looper 代替默認的 Looper
*/
public Handler(Looper looper) {
this(looper, null, false);
}
/**
* Use the provided {@link Looper} instead of the default one and take a callback
* interface in which to handle messages.
*
* @param looper The looper, must not be null.
* @param callback The callback interface in which to handle messages, or null.
* 使用傳入的 Looper 代替默認的碗旅,并且傳入 Callback 用于處理 Message
*/
public Handler(Looper looper, Callback callback) {
this(looper, callback, false);
}
/**
* Use the {@link Looper} for the current thread
* 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 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
* 使用當前線程默認的 Looper 并且設置該 Handler 是否需要是異步的。
* Handler 是默認同步的镜悉,除非你調(diào)用該構(gòu)造函數(shù)設置表示使用異步扛芽。
* 異步消息相對于同步消息,不需要對消息進行同步排序积瞒,并且不受 MessageQueue 的 enqueueSyncBarrier(long) 所帶來的同步障礙影響川尖。
*/
public Handler(boolean async) {
this(null, async);
}
前面所有的構(gòu)造函數(shù)本質(zhì)上都是調(diào)用了下面兩個構(gòu)造函數(shù),我們來看看構(gòu)造函數(shù)到底做了什么茫孔?
/**
* 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.
* 使用當前默認 Looper 和傳入的 Callback 叮喳,傳入 boolean 來設置該 Handler 是否需要是異步的。
* @hide
*/
public Handler(Callback callback, boolean async) {
// 如果有內(nèi)存泄漏危險
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
// 打印警告 log"該 Handler 應該為 static 缰贝,否則可能會導致內(nèi)存泄漏"
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}
// 獲取 Looper
mLooper = Looper.myLooper();
if (mLooper == null) { // 獲取到為 null 拋出異常 “無法在線程內(nèi)部創(chuàng)建沒有調(diào)用
// Looper.prepare() 方法的 handler”
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
// 從 Looper 獲取 MessageQueue
mQueue = mLooper.mQueue;
// mCallback賦值
mCallback = callback;
// mAsynchronous 賦值
mAsynchronous = async;
}
/**
* Use the provided {@link Looper} instead of the default one and take a callback
* interface in which to handle messages. Also 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 looper The looper, must not be null.
* @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
* 添加了 Looper 參數(shù)指定 Looper馍悟,其余跟上一個一樣。
*/
public Handler(Looper looper, Callback callback, boolean async) {
mLooper = looper;
mQueue = looper.mQueue;
mCallback = callback;
mAsynchronous = async;
}
下面是各個方法的解讀
getMessageName(Message message):
該方法返回一個 Message 的指定名稱(大概就是說返回一個唯一標識)剩晴。默認返回Message 的 Callback 的類名锣咒,如果 message.callback 為空,返回 Message.what 的十六進制數(shù)赞弥。obtainMessage():
該方法從全局的 Message Pool(消息池)返回一個 Message 對象毅整。這樣的效率比重新創(chuàng)建一個實例要高。并設置 Message.target == this绽左。如果你不想用這種方法悼嫉,可以直接調(diào)用 Message.obtain()。obtainMessage(int what):
和 obtainMessage() 一樣拼窥,但是給返回的 Message 對象設置了 what 戏蔑。obtainMessage(int what, Object obj):
obtainMessage(int what, int arg1, int arg2):
obtainMessage(int what, int arg1, int arg2, Object obj):
與 obtainMessage() 相同蹋凝,但是設置了 whta 和 obj 兩個屬性。public final boolean post(Runnable r):
添加 Runnable 對象到 MessageQueue 中总棵。該 Runnable 對象將會在該 Handler bain綁定到的 Thread 上運行鳍寂。public final boolean postAtTime(Runnable r, long uptimeMillis):
添加 Runnable 對象到 MessageQueue 中。該 Runnable 對象將會在給定的時間點運行情龄。返回:true 表示該 Runnable 成功添加到 MessageQueue 中伐割,否則返回 false,通常是因為 Looper 處理 MessageQueue 正在退出刃唤。注意隔心,返回 true 并不意味著該 Runnable 一定會被執(zhí)行 -- 如果 Looper 在 Message 送達時間之前 quit,那么 Message 將會丟失尚胞。