Looper在消息機制中的作用是從MessageQueue中取消息以及讓Handler去處理這個消息
在Looper類上面有一段說明,意思是消息機制在線程中的使用示例
* class LooperThread extends Thread {
* public void handleMessage(Message msg) {
* // process incoming messages here
先是執行Looper.prepare---新建一個Looper並保存在ThreadLocal中
然後創建Handler---通過Looper.myLooper方法得到當前線程的Looper,並再一步得到messagequeue
最後執行Looper.loop方法開啓循環讀取消息,消息機制開始生效
Looper.prepare
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();//作用保存Looper到當前線程
private static Looper sMainLooper; // 主線程Looper
final MessageQueue mQueue;//消息隊列
public static void prepare() {
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));
可以看到prepare最終調用的是prepare(boolean quitAllowed)方法
quitAllowed意思是 是否允許終止Messagequeue的循環
從prepare(boolean quitAllowed)我們可以看到一個線程最多隻能擁有一個Looper對象,
爲空則創建一個Looper對象並且保存到sThreadLocal中去
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
在構造函數中 創建了一個Messagequeue對象,並且得到了當前線程
new MessageQueue(quitAllowed)中的quitAllowed表示當前的消息鏈表查詢能否允許被停止
prepare方法的作用是創建一個Looper保存在當前線程ThreadLocal中,並且完成Looper與MessageQueue的綁定
在Android主線程開啓消息機制中調用的是prepareMainLooper()
public static void prepareMainLooper() {
throw new IllegalStateException("The main Looper has already been prepared.");
可以看到主線程調用的prepare(boolean quitAllowed)中的quitAllowed值爲false,這意味着主線程的Messagequeue不能被停止,也就是主線程的消息機制不會被停止
public static @Nullable Looper myLooper() {
public static Looper getMainLooper() {
Looper.loop
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
Message msg = queue.next(); // might block
// No message indicates that the message queue is quitting.
msg.target.dispatchMessage(msg);
從loop方法可以看出裏面是在不斷循環的從Messagequeue隊列中取出消息,只有當queue.next返回null時,循環停止,退出loop方法,消息機制失效
如果queue.next不返回null的話 則調用被取出的msg.target.dispatchMessage 回到msg.target指向的handler中處理
總結
Looper主要是完成了在當前線程中創建對象並且保存在ThreadLocal中,完成Looper與MessageQueue的綁定關聯,