Android handle-message的發(fā)送與處理案例詳解
1、Handle,MessageQueue,Message類圖
Handle: 處理消息,并提供一系列函數(shù)幫忙我們創(chuàng)建消息和插入消息到消息隊(duì)列中
創(chuàng)建handle實(shí)例--PbapClientConnectionHandler
mHandlerThread = new HandlerThread("PBAP PCE handler", Process.THREAD_PRIORITY_BACKGROUND);
mHandlerThread.start();
//將這個(gè)線程設(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ù)處理來(lái)自該消息隊(duì)列的消息。
Android 系統(tǒng)的消息隊(duì)列和消息循環(huán)都是針對(duì)具體線程的,一個(gè)線程可以存在(當(dāng)然也可以不存在)一個(gè)消息隊(duì)列和一個(gè)消息循環(huán)(Looper),特定線程的消息只能分發(fā)給本線程,不能進(jìn)行跨線程通訊。但是創(chuàng)建的工作線程默認(rèn)是沒有消息循環(huán)和消息隊(duì)列的,如果想讓該線程具有消息隊(duì)列和消息循環(huán),需要在線程中首先調(diào)用Looper.prepare()來(lái)創(chuàng)建消息隊(duì)列,然后調(diào)用Looper.loop()進(jìn)入消息循環(huán)
MessageQueue:消息隊(duì)列,Handle和Looper中使用的是同一個(gè)消息隊(duì)列

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對(duì)象
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue;//取得Looper對(duì)象的消息隊(duì)列
// 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 取消息隊(duì)列中的一個(gè)待處理消息
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來(lái)處理消息
handleCallback(msg);
} else {
if (mCallback != null) {
//Handle的全局Callback不為空
if (mCallback.handleMessage(msg)) {
return;
}
}
//調(diào)用handle子類的handleMessage來(lái)處理消息
handleMessage(msg);
}
}
Message.callback用法:將Runnable當(dāng)做一個(gè)Message
Runnable線程處理使用實(shí)例
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 中兩個(gè)Activity 之間的傳值問題
這篇文章主要介紹了Android 中兩個(gè)Activity 之間的傳值問題的相關(guān)資料,需要的朋友可以參考下2017-08-08
給Android初學(xué)者的Gradle知識(shí)普及
Android 驗(yàn)證碼功能實(shí)現(xiàn)代碼
Android5.0之Activity的轉(zhuǎn)場(chǎng)動(dòng)畫的示例

