Handler的创建需要绑定一个Looper对象和Looper对象的MessageQueen,默认调用mLooper = Looper.myLooper(),会返回当前线程中的Looper对象。
/**
* Return the Looper object associated with the current thread. Returns
* null if the calling thread is not associated with a Looper.
*/
public static @Nullable Looper myLooper() {
return sThreadLocal.get();
}
Handler发送消息到MessageQueen中,Looper对象取出MessageQueen中的meesage,发送给Handler处理。
for (;;) {
Message msg = queue.next(); // might block
msg.target.dispatchMessage(msg);
}
创建looper sample
* class LooperThread extends Thread {
* public Handler mHandler;
*
* public void run() {
* Looper.prepare();
*
* mHandler = new Handler() {
* public void handleMessage(Message msg) {
* // process incoming messages here
* }
* };
*
* Looper.loop();
* }