一、
* 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(); * } * }
上述代码的执行流程为
1.Looper.prepare()会执行sThreadLocal.set(new Looper(quitAllowed))
2.而new Looper(quitAllowed)会执行,
(1)mQueue = new MessageQueue(quitAllowed);
(2)mThread = Thread.currentThread();
3.new Handler()会执行
(1)mLooper = Looper.myLooper();
(2)mQueue = mLooper.mQueue;
4.而myLooper();会执行return sThreadLocal.get();,所以保证handler当前线程只有一个looper
5.Looper.loop();会执行
(1)final MessageQueue queue = me.mQueue;
(2)for循环Message msg = queue.next();
(3)msg.target.dispatchMessage(msg);