Android Handle原理(Looper,Handler和Message)三者關(guān)系案例詳解
介紹
前面的內(nèi)容對Handler做了介紹,也講解了如何使用handler,但是我們并不知道他的實現(xiàn)原理。本文從源碼的角度來分析如何實現(xiàn)的。
首先我們得知道Handler,Looper,Message Queue三者之間的關(guān)系
- Handler封裝了消息的發(fā)送,也負(fù)責(zé)接收消。內(nèi)部會跟Looper關(guān)聯(lián)。
- Looper 消息封裝的載,內(nèi)部包含了MessageQueue,負(fù)責(zé)從MessageQueue取出消息,然后交給Handler處理
- MessageQueue 就是一個消息隊列,負(fù)責(zé)存儲消息,有消息過來就存儲起來,Looper會循環(huán)的從MessageQueue讀取消息。
源碼分析
當(dāng)我們new一個Handler對象的時候,看看他的構(gòu)造方法里面做了什么.
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; }
從源碼中我們看到他會調(diào)用Looper.myLooper方法獲取一個Looper對象,然后從Looper對象獲取到MessageQueue對象。
Looper myLooper()
跟進(jìn)去看看Looper.myLooper()方法做了什么。這是一個靜態(tài)方法,可以類名.方法名直接調(diào)用。
public static @Nullable Looper myLooper() { return sThreadLocal.get(); }
這個方法里面就一行代碼,從sThreadLocal中獲取一個Looper對象,sThreadLocal是一個ThreadLocal對象,可以在一個線程中存儲變量。底層是ThreadLocalMap,既然是Map類型那肯定得先set一個Looper對象,然后我們才能從sThreadLocal對象里面get一個Looper對象。
ActivityThread main()
說到這里得給大家介紹一個新的類ActivityThread,ActivityThread類是Android APP進(jìn)程的初始類,它的main函數(shù)是這個APP進(jìn)程的入口。我們看看這個main函數(shù)干了什么事情。
public static final void main(String[] args) { ------ Looper.prepareMainLooper(); if (sMainThreadHandler == null) { sMainThreadHandler = new Handler(); } ActivityThread thread = new ActivityThread(); thread.attach(false); if (false) { Looper.myLooper().setMessageLogging(new LogPrinter(Log.DEBUG, "ActivityThread")); } Looper.loop(); ----- }
在第二行代碼調(diào)用Looper.prepareMainLooper()方法,第13行調(diào)用了Looper.loop()方法。
Looper prepareMainLooper()
繼續(xù)跟進(jìn)Looper.prepareMainLooper()方法,在這個方法中第一行代碼調(diào)用了內(nèi)部的prepare方法。prepareMainLooper有點像單例模式中的getInstance方法,只不過getInstance會當(dāng)時返回一個對象,而prepareMainLooper會新建一個Looper對象,存儲在sThreadLocal中。
public static void prepareMainLooper() { prepare(false); synchronized (Looper.class) { if (sMainLooper != null) { throw new IllegalStateException("The main Looper has already been prepared."); } sMainLooper = myLooper(); } }
Looper prepare()
繼續(xù)跟進(jìn)prepare方法,看第5行代碼,新建了一個Looper對象,調(diào)用sThreadLocal.set方法把Looper對象保存起來??吹竭@里我想聰明的你們肯定明白了為什么new Handler對象的時候調(diào)用Looper.myLooper()方法能從sThreadLocal對象中取到Looper對象。
private static void prepare(boolean quitAllowed) { if (sThreadLocal.get() != null) { throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper(quitAllowed)); }
Looper 構(gòu)造方法
文章開頭我們就講到Looper內(nèi)部包含了MessageQueue,其實就是在new Looper對象的時候就new了一個MessageQueue對象。
private Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); mThread = Thread.currentThread(); }
Looper loop()
ActivityThread類main方法中調(diào)用了Looper的兩個方法,前面我們解釋了prepareMainLooper(),現(xiàn)在來看第二個方法loop()。
public static void loop() { final Looper me = myLooper();//獲取Looper對象 if (me == null) { throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); } final MessageQueue queue = me.mQueue;//從Looper對象獲取MessageQueue對象 // 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 (;;) {//死循環(huán) 一直從MessageQueue中遍歷消息 Message msg = queue.next(); // might block if (msg == null) { return; } // This must be in a local variable, in case a UI event sets the logger final Printer logging = me.mLogging; if (logging != null) { logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } final long traceTag = me.mTraceTag; if (traceTag != 0 && Trace.isTagEnabled(traceTag)) { Trace.traceBegin(traceTag, msg.target.getTraceName(msg)); } try { //調(diào)用handler的dispatchMessage方法,把消息交給handler處理 msg.target.dispatchMessage(msg); } finally { if (traceTag != 0) { Trace.traceEnd(traceTag); } } if (logging != null) { logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); } // Make sure that during the course of dispatching the // identity of the thread wasn't corrupted. final long newIdent = Binder.clearCallingIdentity(); if (ident != newIdent) { Log.wtf(TAG, "Thread identity changed from 0x" + Long.toHexString(ident) + " to 0x" + Long.toHexString(newIdent) + " while dispatching to " + msg.target.getClass().getName() + " " + msg.callback + " what=" + msg.what); } msg.recycleUnchecked(); } }
這個方法的代碼呢比較多。我都給代碼加上了注釋。其實就是一個死循環(huán),一直會從MessageQueue中取消息,如果取到了消息呢,會執(zhí)行msg.target.dispatchMessage(msg)這行代碼,msg.target就是handler,其實就是調(diào)用handler的dispatchMessage方法,然后把從MessageQueue中取到的message傳入進(jìn)去。
Handler dispatchMessage()
public void dispatchMessage(Message msg) { //如果callback不為空,說明發(fā)送消息的時候是post一個Runnable對象 if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) {//這個是用來攔截消息的 if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg);//最終調(diào)用我們重寫的handleMessage方法 } }
這個方法對消息做最后處理,如果是post類型調(diào)用handleCallback方法處理,如果是sendMessage發(fā)送的消息。看我們有沒有攔截消息,如果沒有最終調(diào)用handleMessage方法處理。
Handler handleCallback()
看到這里我們知道為什么post一個Runnable對象,run方法執(zhí)行的代碼在主線程了吧,因為底層根本就沒有開啟線程,就只是調(diào)用了run方法而已。
private static void handleCallback(Message message) { message.callback.run(); }
前面我們從創(chuàng)建handler對象開始,以及創(chuàng)建Looper,創(chuàng)建MessageQueue的整個流程,現(xiàn)在來分析下,當(dāng)我們調(diào)用post以及sendMessage方法時,怎么把Message添加到MessageQueue。
Handler post()
調(diào)用了getPostMessage方法,把Runnable傳遞進(jìn)去。
public final boolean post(Runnable r) { return sendMessageDelayed(getPostMessage(r), 0); }
Handler getPostMessage()
首先調(diào)用Message.obtain()方法,取出一個Message對象,這個方法之前有講過,然后把Runnable對象賦值了Message對象的callback屬性。看到這里我們也明白了dispatchMessage方法為什么要先判斷callback是否為空了吧。
private static Message getPostMessage(Runnable r) { Message m = Message.obtain(); m.callback = r; return m; }
Handler enqueueMessage()
在post方法里面調(diào)用了sendMessageDelayed方法,其實最終調(diào)用的是enqueueMessage方法,所以我這里就直接看enqueueMessage方法源碼。第一行代碼就把handler自己賦值給messgae對象的target屬性。然后調(diào)用MessageQueue的enqueueMessage方法把當(dāng)前的Messgae添加進(jìn)去。
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { msg.target = this; if (mAsynchronous) { msg.setAsynchronous(true); } return queue.enqueueMessage(msg, uptimeMillis); }
總結(jié)
總結(jié):handler負(fù)責(zé)發(fā)送消息,Looper負(fù)責(zé)接收Handler發(fā)送的消息,并直接把消息回傳給Handler自己。MessageQueue就是一個存儲消息的容器。
到此這篇關(guān)于Android Handle原理(Looper,Handler和Message三者關(guān)系案例詳解的文章就介紹到這了,更多相關(guān)Android Handle原理(Looper,Handler和Message三者關(guān)系內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開發(fā)案例手冊Application跳出dialog
這篇文章主要為大家介紹了Android開發(fā)案例手冊Application跳出dialog,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06最新Android版本、代號、對應(yīng)API/NDK級別、發(fā)布時間及市場份額
這篇文章主要介紹了最新Android版本、代號、對應(yīng)API/NDK級別、發(fā)布時間及市場份額,在開發(fā)Android應(yīng)用時會碰到如何選擇Android版本級API級別的問題,看完這篇文章后相信這個問題會迎刃而解2017-12-12Android貝塞爾曲線實現(xiàn)加入購物車拋物線動畫
這篇文章主要為大家詳細(xì)介紹了Android貝塞爾曲線實現(xiàn)加入購物車拋物線動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06Android webview注入JS代碼 修改網(wǎng)頁內(nèi)容操作
這篇文章主要介紹了Android webview注入JS代碼 修改網(wǎng)頁內(nèi)容操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android開發(fā)系列二之窗口Activity的生命周期
這篇文章主要介紹了Android學(xué)習(xí)系列二之窗口Activity的生命周期的相關(guān)資料,需要的朋友可以參考下2016-05-05Android RecyclerView的Item點擊事件實現(xiàn)整理
這篇文章主要介紹了Android RecyclerView的Item點擊事件實現(xiàn)整理的相關(guān)資料,需要的朋友可以參考下2017-01-01Android編程實現(xiàn)動態(tài)更新ListView的方法
這篇文章主要介紹了Android編程實現(xiàn)動態(tài)更新ListView的方法,結(jié)合實例形式詳細(xì)分析了ListView的布局及動態(tài)更新實現(xiàn)方法,需要的朋友可以參考下2016-02-02ReactNative (API)AsyncStorage存儲詳解及實例
這篇文章主要介紹了ReactNative (API)AsyncStorage存儲詳解及實例的相關(guān)資料,需要的朋友可以參考下2016-10-10