Android handle-message的發(fā)送與處理案例詳解
1、Handle,MessageQueue,Message類圖
Handle: 處理消息,并提供一系列函數(shù)幫忙我們創(chuàng)建消息和插入消息到消息隊列中
創(chuàng)建handle實例--PbapClientConnectionHandler
mHandlerThread = new HandlerThread("PBAP PCE handler", Process.THREAD_PRIORITY_BACKGROUND); mHandlerThread.start(); //將這個線程設(shè)置為消息處理Looper線程 mConnectionHandler = new PbapClientConnectionHandler.Builder().setLooper(mHandlerThread.getLooper()).setContext(mService).setClientSM(PbapClientStateMachine.this).setRemoteDevice(mCurrentDevice).build();
Looper作用:Looper的prepare函數(shù)將Looper和調(diào)用prepare的線程綁定在一起,調(diào)用線程調(diào)用loop函數(shù)處理來自該消息隊列的消息。
Android 系統(tǒng)的消息隊列和消息循環(huán)都是針對具體線程的,一個線程可以存在(當(dāng)然也可以不存在)一個消息隊列和一個消息循環(huán)(Looper),特定線程的消息只能分發(fā)給本線程,不能進(jìn)行跨線程通訊。但是創(chuàng)建的工作線程默認(rèn)是沒有消息循環(huán)和消息隊列的,如果想讓該線程具有消息隊列和消息循環(huán),需要在線程中首先調(diào)用Looper.prepare()來創(chuàng)建消息隊列,然后調(diào)用Looper.loop()進(jìn)入消息循環(huán)
MessageQueue:消息隊列,Handle和Looper中使用的是同一個消息隊列
2、發(fā)送消息
3、處理消息
looper處理消息:
loop 使消息循環(huán)起作用,取消息,處理消息
/** * Run the message queue in this thread. Be sure to call * {@link #quit()} to end the loop. */ public static void loop() { final Looper me = myLooper();//返回保存在調(diào)用線程TLV中的Looper對象 if (me == null) { throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); } final MessageQueue queue = me.mQueue;//取得Looper對象的消息隊列 // 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 (;;) { Message msg = queue.next(); // might block 取消息隊列中的一個待處理消息 if (msg == null) { // No message indicates that the message queue is quitting. return; } // This must be in a local variable, in case a UI event sets the logger Printer logging = me.mLogging; if (logging != null) { logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } msg.target.dispatchMessage(msg);//調(diào)用該消息的Handle,交給它的dispatchMessage函數(shù)處理 } }
Handle -dispatchMessage
/** * Handle system messages here. */ public void dispatchMessage(Message msg) { if (msg.callback != null) { //Message的callback不為空,則直接調(diào)用Message的callback來處理消息 handleCallback(msg); } else { if (mCallback != null) { //Handle的全局Callback不為空 if (mCallback.handleMessage(msg)) { return; } } //調(diào)用handle子類的handleMessage來處理消息 handleMessage(msg); } }
Message.callback用法:將Runnable當(dāng)做一個Message
Runnable線程處理使用實例
mHandler.post(new Runnable() { @Override public void run() { final IBinder b = callbacks.asBinder(); }); }
到此這篇關(guān)于Android handle-message的發(fā)送與處理案例詳解的文章就介紹到這了,更多相關(guān)Android handle-message內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android view自定義實現(xiàn)動態(tài)進(jìn)度條
這篇文章主要介紹了Android view自定義實現(xiàn)動態(tài)進(jìn)度條的相關(guān)資料,這里提供實例代碼及實現(xiàn)效果圖,需要的朋友可以參考下2016-12-12android實現(xiàn)主動連接和被動連接的藍(lán)牙聊天功能
這篇文章主要為大家詳細(xì)介紹了android實現(xiàn)主動連接和被動連接的藍(lán)牙聊天功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06Android DrawerLayout實現(xiàn)側(cè)拉菜單功能
這篇文章主要介紹了Android DrawerLayout實現(xiàn)側(cè)拉菜單功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-06-06Android自定義view 你所需要知道的基本函數(shù)總結(jié)
這篇文章主要介紹了Android自定義view 你所需要知道的基本函數(shù)的相關(guān)資料,需要的朋友可以參考下2017-02-02Android應(yīng)用開發(fā)中自定義ViewGroup視圖容器的教程
這篇文章主要介紹了Android應(yīng)用開發(fā)中自定義ViewGroup視圖容器的教程,重點在于View之間的參數(shù)傳遞,文中還講到了使用ViewDragHelper自定義ViewGroup的方法,需要的朋友可以參考下2016-04-04Android ScrollView 下嵌套 ListView 或 GridView出現(xiàn)問題解決辦法
這篇文章主要介紹了ScrollView 下嵌套 ListView 或 GridView 會發(fā)列表現(xiàn)數(shù)據(jù)只能顯示一行。因為他們都是滾動結(jié)構(gòu),兩個滾動條放到一起就會引起沖突,這里提供解決辦法相關(guān)資料,需要的朋友可以參考下2017-07-07