成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Handler、Looper與MessageQueue源碼分析

移動開發(fā) Android
在Android中可以通過Handler來更新主線程中UI的變化,更新UI只能在主線程中進行更新,而為了讓其他線程也能控制UI的變化,Android提供了一種機制Handler、Looper與MessageQueue一同協(xié)作來達到其他線程更新UI的目的。

在Android中可以通過Handler來更新主線程中UI的變化,更新UI只能在主線程中進行更新,而為了讓其他線程也能控制UI的變化,Android提供了一種機制Handler、Looper與MessageQueue一同協(xié)作來達到其他線程更新UI的目的。

一般我們會在主線程中通過如下方法定義一個Handler

  1. private Handler mHandler = new Handler() { 
  2.         @Override 
  3.         public void handleMessage(Message msg) { 
  4.             tv.setText("mHandler change UI"); 
  5.             super.handleMessage(msg); 
  6.         } 
  7.     };  

一般都見不到Looper與MessageQueue的,那么它們都是在哪里調用與如何協(xié)作的呢?在主線程不會顯式的調用Looper而是會在ActivityThread.main方法中默認調用。

  1. public static void main(String[] args) { 
  2.          Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain"); 
  3.         SamplingProfilerIntegration.start(); 
  4.   
  5.         // CloseGuard defaults to true and can be quite spammy.  We 
  6.         // disable it here, but selectively enable it later (via 
  7.         // StrictMode) on debug builds, but using DropBox, not logs. 
  8.         CloseGuard.setEnabled(false); 
  9.   
  10.         Environment.initForCurrentUser(); 
  11.   
  12.         // Set the reporter for event logging in libcore 
  13.         EventLogger.setReporter(new EventLoggingReporter()); 
  14.   
  15.         // Make sure TrustedCertificateStore looks in the right place for CA certificates 
  16.         final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId()); 
  17.          TrustedCertificateStore.setDefaultUserDirectory(configDir); 
  18.   
  19.         Process.setArgV0("<pre-initialized>"); 
  20.   
  21.         Looper.prepareMainLooper();//創(chuàng)建Looper 
  22.   
  23.         ActivityThread thread = new ActivityThread(); 
  24.         thread.attach(false); 
  25.   
  26.         if (sMainThreadHandler == null) { 
  27.             sMainThreadHandler = thread.getHandler(); 
  28.         } 
  29.   
  30.         if (false) { 
  31.             Looper.myLooper().setMessageLogging(new 
  32.                     LogPrinter(Log.DEBUG, "ActivityThread")); 
  33.         } 
  34.   
  35.         // End of event ActivityThreadMain. 
  36.          Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); 
  37.         Looper.loop();//開啟Looper循環(huán) 
  38.   
  39.         throw new RuntimeException("Main thread loop unexpectedly exited"); 
  40.     }  

如上代碼,調用了Looper.prepareMainLooper()方法,在主線程中創(chuàng)建了一個Looper,不信的話我們再查看該方法做了什么

Looper

prepare 

  1. public static void prepare() { 
  2.         prepare(true); 
  3.     } 
  4.   
  5.     private static void prepare(boolean quitAllowed) { 
  6.         if (sThreadLocal.get() != null) { 
  7.             throw new RuntimeException("Only one Looper may be created per thread"); 
  8.         } 
  9.         sThreadLocal.set(new Looper(quitAllowed));//創(chuàng)建Looper并賦給sThreadLocal 
  10.     } 
  11.   
  12.     /** 
  13.      * Initialize the current thread as a looper, marking it as an 
  14.      * application's main looper. The main looper for your application 
  15.      * is created by the Android environment, so you should never need 
  16.      * to call this function yourself.  See also: {@link #prepare()} 
  17.      */ 
  18.     public static void prepareMainLooper() { 
  19.         prepare(false); 
  20.         synchronized (Looper.class) { 
  21.             if (sMainLooper != null) { 
  22.                 throw new IllegalStateException("The main Looper has already been prepared."); 
  23.             } 
  24.             sMainLooper = myLooper(); 
  25.         } 
  26.     } 
  27.       
  28.     public static @Nullable Looper myLooper() { 
  29.         return sThreadLocal.get(); 
  30.     }  

在prepareMainLooper方法中調用了prepare而通過prepare會發(fā)現(xiàn)它其實就是創(chuàng)建了一個Looper,并把它賦給了sThreadLocal。同時可以通過myLooper方法獲取當前線程中的Looper。再來看下new Looper(quitAllowed)初始化了什么

  1. private Looper(boolean quitAllowed) { 
  2.         mQueue = new MessageQueue(quitAllowed); 
  3.         mThread = Thread.currentThread(); 
  4.     }  

在這里我們終于看到了MessageQueue了,它創(chuàng)建了一個MessageQueue。該消息隊列就是用來保存后續(xù)的Message。再回到ActivityThread.main方法中,發(fā)現(xiàn)它調用了Looper.loop()是用來開啟Looper循環(huán)的,監(jiān)聽消息隊列MessageQueue中的消息。

loop

我們來看下Looper.loop()的源碼:

  1. public static void loop() { 
  2.         final Looper me = myLooper();//獲取Looper 
  3.         if (me == null) { 
  4.             throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); 
  5.         } 
  6.         final MessageQueue queue = me.mQueue;//獲取消息隊列 
  7.   
  8.         // Make sure the identity of this thread is that of the local process, 
  9.         // and keep track of what that identity token actually is
  10.         Binder.clearCallingIdentity(); 
  11.         final long ident = Binder.clearCallingIdentity(); 
  12.   
  13.         for (;;) {         
  14.             Message msg = queue.next(); // might block 
  15.             if (msg == null) { 
  16.                 // No message indicates that the message queue is quitting. 
  17.                 return
  18.             } 
  19.   
  20.             // This must be in a local variable, in case a UI event sets the logger 
  21.             final Printer logging = me.mLogging; 
  22.             if (logging != null) { 
  23.                 logging.println(">>>>> Dispatching to " + msg.target + " " + 
  24.                         msg.callback + ": " + msg.what); 
  25.             } 
  26.   
  27.             final long traceTag = me.mTraceTag; 
  28.             if (traceTag != 0) { 
  29.                 Trace.traceBegin(traceTag, msg.target.getTraceName(msg)); 
  30.             } 
  31.             try { 
  32.                 msg.target.dispatchMessage(msg);//通過Handler分發(fā)消息 
  33.             } finally { 
  34.                 if (traceTag != 0) { 
  35.                     Trace.traceEnd(traceTag); 
  36.                 } 
  37.             } 
  38.   
  39.             if (logging != null) { 
  40.                 logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); 
  41.             } 
  42.   
  43.             // Make sure that during the course of dispatching the 
  44.             // identity of the thread wasn't corrupted. 
  45.             final long newIdent = Binder.clearCallingIdentity(); 
  46.             if (ident != newIdent) { 
  47.                 Log.wtf(TAG, "Thread identity changed from 0x" 
  48.                         + Long.toHexString(ident) + " to 0x" 
  49.                         + Long.toHexString(newIdent) + " while dispatching to " 
  50.                         + msg.target.getClass().getName() + " " 
  51.                         + msg.callback + " what=" + msg.what); 
  52.             } 
  53.   
  54.             msg.recycleUnchecked(); 
  55.         } 
  56.     }  

在loop中首先獲取了當前所在線程的Looper,同時也獲取到了Looper中的MessageQueue,說明Looper已經(jīng)與當前的線程進行了綁定。在后面開啟了一個for的死循環(huán),發(fā)現(xiàn)它做的事件是不斷的從消息隊列中取出消息,***都交給msg.target調用它的dispatchMessage方法,那么target又是什么呢?我們進入Message

Message

  1. /*package*/ int flags; 
  2.  
  3.     /*package*/ long when
  4.      
  5.     /*package*/ Bundle data; 
  6.      
  7.     /*package*/ Handler target; 
  8.      
  9.     /*package*/ Runnable callback; 
  10.      
  11.     // sometimes we store linked lists of these things 
  12.     /*package*/ Message next 

發(fā)現(xiàn)它就是我們熟悉的Handler,說明***調用的就是Handler中的dispatchMessage方法,對消息的分發(fā)處理。這樣一來Handler就通過Looper聯(lián)系上了Looper所綁定的線程,即為主線程。

Handler

  1. public Handler(Callback callback, boolean async) { 
  2.         if (FIND_POTENTIAL_LEAKS) { 
  3.             final Class<? extends Handler> klass = getClass(); 
  4.             if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && 
  5.                     (klass.getModifiers() & Modifier.STATIC) == 0) { 
  6.                 Log.w(TAG, "The following Handler class should be static or leaks might occur: " + 
  7.                     klass.getCanonicalName()); 
  8.             } 
  9.         } 
  10.   
  11.         mLooper = Looper.myLooper(); 
  12.         if (mLooper == null) { 
  13.             throw new RuntimeException( 
  14.                 "Can't create handler inside thread that has not called Looper.prepare()"); 
  15.         } 
  16.         mQueue = mLooper.mQueue; 
  17.         mCallback = callback; 
  18.         mAsynchronous = async; 
  19.     }  

通過Handler的初始化,它獲取了它所處線程的Looper,同時也獲取了Looper中的消息隊列。當然如果所處線程的Looper為空的話就會拋出異常,這就解釋了為什么在非主線程中創(chuàng)建Handler要分別調用Looper.prepare與Looper.loop而主線程則不需要,因為它默認已經(jīng)調用了。

dispatchMessage

  1. public void dispatchMessage(Message msg) { 
  2.         if (msg.callback != null) { 
  3.             handleCallback(msg); 
  4.         } else { 
  5.             if (mCallback != null) { 
  6.                 if (mCallback.handleMessage(msg)) { 
  7.                     return
  8.                 } 
  9.             } 
  10.             handleMessage(msg); 
  11.         } 
  12.     } 
  13. private static void handleCallback(Message message) { 
  14.         message.callback.run(); 
  15.     }     

回到前面,對于dispatchMessage的處理,首先判斷msg.callback是否為空,這里callback通過上面的Message應該能知道他就是一個Runnable,如果不為空則直接調用Runnable的run方法。否則調用Handler的handleMessage方法.而這個方法相信大家已經(jīng)很熟悉了,對事件的處理都是在這個方法中執(zhí)行的。因為通過前面我們已經(jīng)知道了Handler已經(jīng)聯(lián)系上了主線程,所以handleMessage中的處理自然相對于在主線程中進行,自然也能更新UI了。通過這里我們能把Looper比作是一個橋梁,來連接Looper所在的線程與Handler之間的通信,同時管理消息隊列MessageQueue中的消息。那么前面的Runnable又是如何不為空的呢?我們使用Handler有兩種方法,一種是直接創(chuàng)建一個Handler并且重寫它的handleMessage方法,而另一種可以通過Handler.post(Runnable)來使用,這樣事件的處理自然就在run方法中實現(xiàn)。

上面介紹了Handler是如何聯(lián)系上了需要操作的線程與對消息是如何取出與處理的。下面來談談消息是如何放入到Looper中的MessageQueue中的。

sendMessageAtTime

通過Handler發(fā)送消息的方式很多,例如:sendMessage、sendEmptyMessage與sendMessageDelayed等,其實到***他們調用的都是sendMessageAtTime方法。所以還是來看下sendMessageAtTime方法中的實現(xiàn)。

  1. public boolean sendMessageAtTime(Message msg, long uptimeMillis) { 
  2.         MessageQueue queue = mQueue; 
  3.         if (queue == null) { 
  4.             RuntimeException e = new RuntimeException( 
  5.                     this + " sendMessageAtTime() called with no mQueue"); 
  6.             Log.w("Looper", e.getMessage(), e); 
  7.             return false
  8.         } 
  9.         return enqueueMessage(queue, msg, uptimeMillis); 
  10.     }  

而sendMessageAtTime則就是調用了enqueueMessage操作,看這方法名就知道是入隊列操作了。

enqueueMessage

  1. private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { 
  2.         msg.target = this; 
  3.         if (mAsynchronous) { 
  4.             msg.setAsynchronous(true); 
  5.         } 
  6.         return queue.enqueueMessage(msg, uptimeMillis); 
  7.     }  

果不其然直接調用了MessageQueue中的queue.enqueueMessage(msg, uptimeMillis)將消息加入消息隊列,同時這段代碼msg.target = this 將當前的Handler賦給了msg.target,這就是前面所說的Looper.loop方法中調用的Handler。這樣就把消息放到了MessageQueue中,進而通過前面所講的loop來取出消息進行相應的處理,這樣就構成了整個對消息進行處理的系統(tǒng)。這也是使用Handler內(nèi)部所發(fā)生的原理。好了Handler、Looper與MessageQueue它們之間的聯(lián)系基本就是這些了。我也簡單畫了張圖希望有所幫助

總結

來總結下它們之間的流程。首先創(chuàng)建Handler而在Handler所處的線程中必須要有一個Looper,如果在主線程中默認幫我們實現(xiàn)了,其他線程必須調用Looper.prepare來創(chuàng)建Looper同時調用Looper.loop開啟對消息的處理。每個Looper中都有一個MessageQueue它是用來存儲Message的,Handler通過post或者send..等一系列操作通過Looper將消息放入到消息隊列中,而Looper通過開啟一個***的循環(huán)來一直監(jiān)聽著消息的處理,不斷從MessageQueue中取出消息,并交給與當前Looper所綁定的handler的dispatchMessage進行分發(fā),***根據(jù)情況調用Runnable的run或者Handler的HandlerMessage方法對消息進行***的處理。

其它分享:https://idisfkj.github.io/arc...

責任編輯:龐桂玉 來源: segmentfault
相關推薦

2011-11-23 09:33:45

HandlerLooperMessage

2014-05-22 14:57:28

Android消息處理機制Looper

2014-05-22 15:15:53

Android消息處理機制Looper

2014-05-22 15:18:25

Android消息處理機制Looper

2014-05-22 15:33:31

Android消息處理機制Looper

2014-05-22 15:45:58

Android消息處理機制Looper

2014-05-22 15:00:16

Android消息處理機制Looper

2014-05-22 15:04:00

Android消息處理機制Looper

2014-05-22 15:07:44

Android消息處理機制Looper

2014-05-22 15:38:27

Android消息處理機制Looper

2014-05-22 15:48:50

Android消息處理機制Looper

2014-05-22 15:41:59

Android消息處理機制Looper

2013-04-11 12:40:16

Android消息機制

2021-08-12 16:28:10

AndroidHandleLooper

2021-09-08 10:47:33

Flink執(zhí)行流程

2016-11-25 13:26:50

Flume架構源碼

2016-11-29 09:38:06

Flume架構核心組件

2016-11-25 13:14:50

Flume架構源碼

2022-08-15 11:28:22

handler注冊過程APiServer

2009-12-22 13:36:39

Linux Sysfs
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲综合精品 | 亚洲欧洲精品在线 | 久久激情五月丁香伊人 | 亚洲欧洲综合av | 国产欧美一区二区三区日本久久久 | 亚洲午夜精品视频 | 久久久国产一区 | 精品欧美一区二区三区精品久久 | 亚洲一区二区久久 | 最新毛片网站 | 丁香色婷婷 | 国产小视频在线观看 | 国产成人免费视频网站高清观看视频 | 色.com| 久久精品国产一区二区电影 | 国产高清一区二区三区 | 精品久久久久久亚洲国产800 | 成人在线电影网站 | 在线免费观看毛片 | 久久精品一区二区三区四区 | 久久久综合精品 | 亚洲一区二区视频 | 亚洲精品一区av在线播放 | 色综合视频 | 男人的天堂在线视频 | 成人免费视频 | 黄色在线免费播放 | 亚洲天堂中文字幕 | 国产精品96久久久久久 | 中文字幕久久久 | av网站免费观看 | 亚洲一区二区三区乱码aⅴ 四虎在线视频 | 国产视频日韩 | 精品一区二区久久久久久久网站 | 日韩精品一区二区三区老鸭窝 | 久草久草久草 | 中文在线a在线 | 综合久久网 | 国产精品成人在线播放 | 日韩精品在线观看网站 | 偷拍自拍第一页 |