Android 訊息處理源碼分析(2)

來源:互聯網
上載者:User

標籤:android源碼分析

 Android 訊息處理源碼分析(1)點擊開啟連結


繼續接著分析剩下的類檔案

Looper.javapublic final class Looper {    final MessageQueue mQueue;   //訊息佇列    final Thread mThread;   //Looper聯絡的線程        public static void prepare() {        prepare(true);    }        private static void prepare(boolean quitAllowed) {   //先會檢查是否有Looper,若有則拋出異常,沒有的話則建立一個Looper執行個體儲存起來        if (sThreadLocal.get() != null) {            throw new RuntimeException("Only one Looper may be created per thread");        }        sThreadLocal.set(new Looper(quitAllowed));    }    public static void prepareMainLooper() {        prepare(false);        synchronized (Looper.class) {            if (sMainLooper != null) {                throw new IllegalStateException("The main Looper has already been prepared.");            }            sMainLooper = myLooper();        }    }   //在這個線程中運行訊息佇列,調用quit()停止   public static void loop() {...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(); // 從訊息佇列中取出一條訊息            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);   //交給msg的handler分發訊息處理...   }    //取出當前線程的Looper,返回空則表示當前線程沒有Looper    public static Looper myLooper() {        return sThreadLocal.get();    }}


Handler.javapublic class Handler {     //定義Callback介面     public interface Callback {      public boolean handleMessage(Message msg);     }     //子類要實現的訊息處理方法     public void handleMessage(Message msg) {     }     * Handle system messages here.     */    public void dispatchMessage(Message msg) {        //分發資訊        if (msg.callback != null) {          //若指定了msg.callback,則由它處理            handleCallback(msg);        } else {            if (mCallback != null) {        //若指定了Handler.mCallback,則由它處理                if (mCallback.handleMessage(msg)) {    //調用mCallback介面的實現方法                    return;                }            }            handleMessage(msg);   最後才調用Handler自身重載的handleMessage方法        }    }    分發訊息函數中,訊息先會檢查自身有沒有處理自身的回調Runnable,若有則由它處理,若沒有則會檢查該handler有無自身的回調處理,若有則調用,若沒有則調用自身重載的handleMessage方法    //Handler的產生總是和它當前所處線程有關的,如果當前線程中沒有一個Looper,則會報錯,UI線程中預設有產生Looper的函數    public Handler() {        this(null, false);    }        //使用指定的Looper(可以處理那個Looper線程中的訊息),不用預設的從當前線程中取出Looper    public Handler(Looper looper) {        this(looper, null, false);    }   ...}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android 訊息處理源碼分析(2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.