<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                #### 10.2.4 Handler的工作原理 **[Handler](https://www.androidos.net.cn/android/6.0.1_r16/xref/frameworks/base/core/java/android/os/Handler.java)的工作主要包含消息的發送和接收過程**。消息的**發送可以通過post的一系列方法以及send的一系列方法來實現,post的一系列方法最終是通過send的一系列方法來實現的**。 發送一條消息的典型過程如下所示。 ``` public final boolean sendMessage(Message msg) { return sendMessageDelayed(msg, 0); } 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); } private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { msg.target = this; if (mAsynchronous) { msg.setAsynchronous(true); } return queue.enqueueMessage(msg, uptimeMillis); } ``` 可以發現,**Handler發送消息的過程僅僅是向消息隊列中插入了一條消息**,**MessageQueue的next方法就會返回這條消息給Looper(Looper.loop方法內部調用了MessageQueue的next方法), Looper收到消息后就開始處理了,最終消息由Looper交由Handler處理,即Handler的dispatchMessage方法會被調用,這時Handler就進入了處理消息的階段**。dispatchMessage的實現如下所示。 ``` public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } } ``` **Handler處理消息的過程**如下: 首先,檢查Message的callback是否為null,不為null就通過handleCallback來處理消息。**Message的callback是一個Runnable對象,實際上就是Handler的post方法所傳遞的Runnable參數**。handleCallback的邏輯也是很簡單,如下所示。 ``` private static void handleCallback(Message message) { message.callback.run(); } ``` 其次,**callback(就是Handler的post方法所傳遞的Runnable參數)為null,再檢查mCallback是否為null,不為null就調用mCallback的handleMessage方法來處理消息**。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對象**:`Handler handler = new Handler(callback)`。那么**Callback的意義是什么呢?源碼里面的注釋已經做了說明:可以用來創建一個Handler的實例但并不需要派生Handler的子類**。 **在日常開發中,創建Handler最常見的方式就是派生一個Handler的子類并重寫其handleMessage方法來處理具體的消息,而Callback給我們提供了另外一種使用Handler的方式,當我們不想派生子類時,就可以通過Callback來實現**。 最后,**調用Handler的handleMessage方法來處理消息**。Handler**處理消息的過程**可以歸納為一個流程圖,如圖10-2所示。 :-: ![](https://img.kancloud.cn/d7/fb/d7fb753a94bd9b8a1fafc12b75c20720_474x613.png) 圖10-2 Handler消息處理流程圖 **Handler還有一個特殊的構造方法,那就是通過一個特定的Looper來構造Handler**,它的實現如下所示。通過這個構造方法可以實現一些特殊的功能。 ``` /** * Use the provided {@link Looper} instead of the default one. * * @param looper The looper, must not be null. */ public Handler(Looper looper) { this(looper, null, false); } ``` 下面看一下Handler的一個默認構造方法public Handler(),這個構造方法會調用下面的構造方法。很明顯,如果當前線程沒有Looper的話,就會拋出“Can't create handler inside thread that has not called Looper.prepare()”這個異常,這也解釋了在沒有Looper的子線程中創建Handler會引發程序異常的原因。 ``` 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; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看