欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android handle-message的發(fā)送與處理案例詳解

 更新時(shí)間:2021年08月27日 15:56:39   作者:靈動(dòng)小溪  
這篇文章主要介紹了Android handle-message的發(fā)送與處理案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

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ù)處理來自該消息隊(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()來創(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來處理消息
        handleCallback(msg);
    } else {
        if (mCallback != null) {
            //Handle的全局Callback不為空
            if (mCallback.handleMessage(msg)) {
                return;
            }
        }
        //調(diào)用handle子類的handleMessage來處理消息
        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)文章

最新評(píng)論