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

來源:互聯網
上載者:User

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


在Android中,通常被使用的訊息佇列的代碼在目錄sourcesandroid-22androidos下,涉及到以下幾個類檔案
Handler.java

Looper.java

Message.java

MessageQueue.java

 

 

Message.javapublic final class Message implements Parcelable {    public int what;    //訊息種類    public int arg1;    //低開銷的整型參數    public int arg2;    public Object obj;  //Object型資料    public Messenger replyTo;  //訊息處理完後通知給寄件者    /*package*/ int flags;   //訊息標記:正在使用和非同步等    /*package*/ long when;   //訊息建立時的時間        /*package*/ Bundle data; //訊息附帶的額外資料        /*package*/ Handler target; //訊息接受者,處理者        /*package*/ Runnable callback; //優先使用回調處理來處理訊息        /*package*/ Message next;   //下一個訊息,形成鏈表    private static Message sPool;    //訊息池中的頭訊息    上面中的target,通常由重新實現的Handler子類的handleMessage函數來處理訊息 public static Message obtain() {     //擷取訊息的函數,如果有訊息的話則擷取出來m,鏈表指標移動一位,否則則返回一條空訊息        synchronized (sPoolSync) {            if (sPool != null) {                Message m = sPool;                sPool = m.next;                m.next = null;                m.flags = 0; // clear in-use flag                sPoolSize--;                return m;            }        }        return new Message();    } public void sendToTarget() {    //發送訊息給處理者        target.sendMessage(this);    //調用Handler.java中的函數    }}

MessageQueue.javapublic final class MessageQueue {Message mMessages;    //當前要處理的訊息//當需要從鏈表中擷取一個訊息時,就會調用next函數,若訊息佇列中沒有訊息,則會阻塞等待,通過調用nativePollOnce函數來完成Message next() {...}boolean enqueueMessage(Message msg, long when) {     //按時間順序添加訊息        if (msg.target == null) {            throw new IllegalArgumentException(Message must have a target.);        }        if (msg.isInUse()) {            throw new IllegalStateException(msg +  This message is already in use.);        }        synchronized (this) {            if (mQuitting) {                IllegalStateException e = new IllegalStateException(                        msg.target +  sending message to a Handler on a dead thread);                Log.w(MessageQueue, e.getMessage(), e);                msg.recycle();                return false;            }            msg.markInUse();            msg.when = when;            Message p = mMessages;            boolean needWake;            if (p == null || when == 0 || when < p.when) {                // New head, wake up the event queue if blocked.                msg.next = p;                mMessages = msg;                needWake = mBlocked;            } else {                // Inserted within the middle of the queue.  Usually we don't have to wake                // up the event queue unless there is a barrier at the head of the queue                // and the message is the earliest asynchronous message in the queue.                needWake = mBlocked && p.target == null && msg.isAsynchronous();                Message prev;                for (;;) {                    prev = p;                    p = p.next;                    if (p == null || when < p.when) {                        break;                    }                    if (needWake && p.isAsynchronous()) {                        needWake = false;                    }                }                msg.next = p; // invariant: p == prev.next                prev.next = msg;            }            // We can assume mPtr != 0 because mQuitting is false.            if (needWake) {                      nativeWake(mPtr);  //調用底層喚醒函數,管道喚醒            }        }        return true;    }        

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.