Handler機制(四)---Handler源碼解析,handler---handler

來源:互聯網
上載者:User

Handler機制(四)---Handler源碼解析,handler---handler

Handler的主要用途有兩個:(1)、在將來的某個時刻執行訊息或一個runnable,(2)把訊息發送到訊息佇列。

   主要依靠post(Runnable)、postAtTime(Runnable, long)、postDelayed(Runnable, long)、sendEmptyMessage(int)、sendMessage(Message)、sendMessageAtTime(Message)、sendMessageDelayed(Message, long)這些方法來來完成訊息調度。post方法是當到Runable對象到達就被插入到訊息佇列;sendMessage方法允許你把一個包含有資訊的Message插入隊列,而且它會Handler的handlerMessage(Message)方法中執行(該方法要求在Handler的子類中實現)。

1.構造方法
 /**   預設的構造方法,handler是和當前線程的隊列關聯在一起,如果隊列不存在,那麼handler就不能接受訊息。     * Default constructor associates this handler with the {@link Looper} for the     * current thread.     * 如果線程沒有looper,就會拋出異常     * If this thread does not have a looper, this handler won't be able to receive messages     * so an exception is thrown.     */public Handler() {        this(null, false);    }//傳入一個callback介面用於處理handler傳遞的Message。 public Handler(Callback callback) {        this(callback, false);    }//給變數賦值  public Handler(Callback callback, boolean async) {        if (FIND_POTENTIAL_LEAKS) {            final Class<? extends Handler> klass = getClass();            if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&                    (klass.getModifiers() & Modifier.STATIC) == 0) {                Log.w(TAG, "The following Handler class should be static or leaks might occur: " +                    klass.getCanonicalName());            }        }        mLooper = Looper.myLooper();        if (mLooper == null) {            throw new RuntimeException(                "Can't create handler inside thread that has not called Looper.prepare()");        }        mQueue = mLooper.mQueue;        mCallback = callback;        mAsynchronous = async;    }    public Handler(Looper looper, Callback callback, boolean async) {        mLooper = looper;        mQueue = looper.mQueue;        mCallback = callback;        mAsynchronous = async;    }

  方法都差不多,主要是完成了賦值的過程,還有幾個沒粘貼,但是都差不多。。。

2.變數
/*     設定這個標記為true來檢測不是靜態匿名,本地或成員類繼承Handler類。這些類型的類可以帶來潛在的泄漏。在Handler的構造方法裡面使用到了這個參數,目的就如上所述。     */    private static final boolean FIND_POTENTIAL_LEAKS = false;    final MessageQueue mQueue;    final Looper mLooper;    final Callback mCallback;  //回調介面    final boolean mAsynchronous;    IMessenger mMessenger;

  接著下面就是Callback介面:

 /**     * Callback interface you can use when instantiating(執行個體化) a Handler to avoid     * having to implement(實現) your own subclass of Handler.     * @param msg A {@link android.os.Message Message} object     * @return True if no further handling is desired     */    public interface Callback {        public boolean handleMessage(Message msg);    }

callback介面你可以在執行個體化的時候用,避免去實現你自己的handler子類

這個Callback介面裡只有一個handleMessage方法返回boolean值,在後面Handler的ctor會用到,一般情況下都是null。這個介面的存在

沒什麼特殊的含義,只是為了讓你不extends Handler就能處理訊息而已(正如此方法的doc所說),類似Thread和Runnable介面的關係。

3.handleMessage
/**     * Subclasses must implement this to receive messages.     */    public void handleMessage(Message msg) {    }

子類需要實現這個方法,因為這是個空方法。

4.dispatchMessage
/**     * Handle system messages here.     */    public void dispatchMessage(Message msg) {        if (msg.callback != null) {            handleCallback(msg);        } else {            if (mCallback != null) {                if (mCallback.handleMessage(msg)) {                    return;                }            }            handleMessage(msg);        }    }private static void handleCallback(Message message) {        message.callback.run();    }

它的處理是如果message自身設定了callback,則

直接調用callback.run()方法,否則Callback介面的作用就顯現了;如果我們傳遞了Callback介面的實現,即mCallback非空,則調用它處理

message,如果處理了,返回true,則直接返回,否則接著調用Handler自己的handleMessage方法,其預設實現是do nothing,如果你

是extends Handler,那麼你應該在你的子類中為handleMessage提供自己的實現。

5.一系列obtainMessage
public final Message obtainMessage()    {        return Message.obtain(this);    }public final Message obtainMessage(int what)    {        return Message.obtain(this, what);    }public final Message obtainMessage(int what, int arg1, int arg2, Object obj)    {        return Message.obtain(this, what, arg1, arg2, obj);    }

從上面可以看出來還是調用的Message的obtain方法,來構造message。

6.一系列postXXX方法:
 /**     * Causes the Runnable r to be added to the message queue.     * The runnable will be run on the thread to which this handler is      * attached.      *       * @param r The Runnable that will be executed.     *      * @return Returns true if the Runnable was successfully placed in to the      *         message queue.  Returns false on failure, usually because the     *         looper processing the message queue is exiting.     */ public final boolean post(Runnable r)    {       return  sendMessageDelayed(getPostMessage(r), 0);    }public final boolean postAtTime(Runnable r, long uptimeMillis)    {        return sendMessageAtTime(getPostMessage(r), uptimeMillis);    } public final boolean postAtTime(Runnable r, Object token, long uptimeMillis)    {        return sendMessageAtTime(getPostMessage(r, token), uptimeMillis);    }public final boolean postDelayed(Runnable r, long delayMillis)    {        return sendMessageDelayed(getPostMessage(r), delayMillis);    } public final boolean postAtFrontOfQueue(Runnable r)    {        return sendMessageAtFrontOfQueue(getPostMessage(r));    }

上面方法的作用就是:把Runnable發送到訊息佇列,執行的時候實行Runnable的run方法。。

下面看一系列sendXXX方法,和上邊對應的。。。

//把訊息入隊public final boolean sendMessageDelayed(Message msg, long delayMillis)    {        if (delayMillis < 0) {            delayMillis = 0;        }        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);    }public boolean sendMessageAtTime(Message msg, long uptimeMillis) {        MessageQueue queue = mQueue;        if (queue == null) {            RuntimeException e = new RuntimeException(                    this + " sendMessageAtTime() called with no mQueue");            Log.w("Looper", e.getMessage(), e);            return false;        }        return enqueueMessage(queue, msg, uptimeMillis);    } public final boolean sendEmptyMessage(int what)    {        return sendEmptyMessageDelayed(what, 0);

  上面所有的postXXX,sendXXX方法最後都會調用這個方法:enqueueMessage

 private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {        msg.target = this;        if (mAsynchronous) {            msg.setAsynchronous(true);        }        return queue.enqueueMessage(msg, uptimeMillis);    }

 最後調用queue的enqueueMessage方法把msg入隊,對這個方法不瞭解的,可以看前邊的MessageQueue源碼解析。

SystemClock.uptimeMillis(),就是表示系統開機到當前的時間總數,如果有延遲,就加上延遲時間,分析到現在,我們也能發現,postDelayed不是延遲多少秒發送訊息,這個訊息是直接發送給隊列的,不過在MessaegQueue中,訊息按時間排放的,不到時間不會把它取出來,所以應該說延遲多少秒取出訊息更合適。。

至於上邊的getPostMessage(r)就是把r設定給callback。

private static Message getPostMessage(Runnable r) {        Message m = Message.obtain();        m.callback = r;        return m;    }
removeCallbacks:

  調用MQ的removeMessages方法。就是移出messageQueue中所有滿足條件的message,當然前提是訊息還沒取走。

  /**     * Remove any pending posts of Runnable r that are in the message queue.     */    public final void removeCallbacks(Runnable r)    {        mQueue.removeMessages(this, r, null);    }    /**     * Remove any pending posts of Runnable <var>r</var> with Object     * <var>token</var> that are in the message queue.  If <var>token</var> is null,     * all callbacks will be removed.     */    public final void removeCallbacks(Runnable r, Object token)    {        mQueue.removeMessages(this, r, token);    }

   分析結束,鼓勵自己下。。哈哈哈。。。。

前三篇傳送門: Looper源碼解析: http://www.cnblogs.com/jycboy/p/5787443.html

                    MessageQueue源碼解析: http://www.cnblogs.com/jycboy/p/5786682.html

                    Message源碼解析:http://www.cnblogs.com/jycboy/p/5786551.html

 

聯繫我們

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