1. 程式人生 > >Android關於looper的幾個方法的個人理解

Android關於looper的幾個方法的個人理解

    最近在看android的looper這個類與android的訊息佇列的處理有一定的緊密關係,今天寫一些關於這個類的幾個常用的方法,主要是起到自己鞏固學習的作用,方便以後自己的檢視。

1、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)); }
    先是判斷了一下sThreadLocal是否為空,不為空時報出異常否側講一個Looper例項放入其中。這也可以從中看出不可以多次的去呼叫這個方法,第一次呼叫後sThreadLocal中就已經是有值的了,如果再次呼叫就會報錯。
2、getMainLooper() 原始碼如下
public static Looper getMainLooper() {
    synchronized (Looper.class) {
        return sMainLooper;
    }
}
該方法主要是返回當前程序中的主執行緒的訊息佇列。
3、loop() 原始碼如下
public static void loop() {
    final Looper me = myLooper();
    if (me == null) {
        throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
    }
    final MessageQueue queue = me.mQueue;

    // Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is. Binder.clearCallingIdentity(); final long ident = Binder.clearCallingIdentity(); 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(); } }
該方法先是判斷myLooper的方法返回的是否為空,為空就報錯證明你沒有向sThreadLocal中放入例項及沒有呼叫到prepare()方法或者呼叫它的方法。而後獲取其訊息佇列,進行無限迴圈。
4、Looper() 原始碼如下
private Looper(boolean quitAllowed) {
    mQueue = new MessageQueue(quitAllowed);
    mThread = Thread.currentThread();
}
Looper類的構造方法,建立了一個訊息佇列。
5、myLooper 原始碼如下
public static @Nullable Looper myLooper() {
    return sThreadLocal.get();
}
返回儲存在sThreadLocal中的looper例項,可以看出在呼叫該方法前,必須呼叫prepare()方法或者呼叫它的方法,不然sThreadLocal中是空的會報錯。
6、quit() 原始碼如下
public void quit() {
    mQueue.quit(false);
}
   停止looper。