【android12】【AHandler】【3.AHandler原理篇AHandler类方法全解】

AHandler系列

【android12】【AHandler】【1.AHandler异步无回复消息原理篇】-优快云博客

【android12】【AHandler】【2.AHandler异步回复消息原理篇】-优快云博客

其他系列

本人系列文章-优快云博客


1.简介

前面两篇我们主要介绍了有回复和无回复的消息的使用方法和源码解析,为了更好的理解Ahandler这个类的作用,本篇便主要对AHandler类的所有方法进行全解。

简单介绍一下Ahandler机制

AHandler是Android native层实现的一个异步消息机制,在这个机制中所有的处理都是异步的,将消息封装到一个消息AMessage结构体中,然后放到消息队列中去,后台专门有一个线程ALooper会从这个队列中取出消息然后分发执行,执行函数就是AHandler实例的onMessageReceived。

在AHandler中的消息分为不需要回复的消息和需要回复的消息。

不需要回复的消息:当被post到消息队列后,Alooper会从消息队列中取出消息,并发送给相应的Ahandler的onMessageReceived进行处理。

需要回复的消息:当被post到消息队列后,Alooper会从消息队列中取出消息,并发送给相应的Ahandler的onMessageReceived进行处理,处理完后,Ahandler会将想回复的消息返回给发送方,发送方接受返回的response消息。

1.1 主要类及其作用

AHandler:处理类,用于消息的处理,有一个纯虚函数onMessageReceived,一般需要继承此类并重写此方法,用于接受和处理消息。

AMessage:消息类,用于构建各种消息,通过post方法将消息发送给Alooper

Alooper:轮询类,用于保存消息队列,然后将消息发送到对应的AHandler

AReplyToken:这类似一个标识,表示要回复的是哪一条消息

 1.2 类图

2.源码解析

2.1 AHandler类

AHandler这个类一般用作子类,需要使用Ahandler机制通信时候,需要继承此类,并重写onMessageReceived这个方法。

struct AHandler : public RefBase {//RefBase类是andorid中用于引用计数的类
    AHandler(): mID(0),mVerboseStats(false),mMessageCounter(0) {}//默认构造函数的实现。

    ALooper::handler_id id() const {//函数后面+const表示常函数。不能修改成员属性的值
        return mID;
    }

    sp<ALooper> looper() const {//获取looper的强智能指针
        return mLooper.promote();
    }

    wp<ALooper> getLooper() const {//获取looper的弱智能指针
        return mLooper;
    }

    wp<AHandler> getHandler() const {
        // allow getting a weak reference to a const handler
        return const_cast<AHandler *>(this);
    }

protected:
    virtual void onMessageReceived(const sp<AMessage> &msg) = 0;//纯虚函数

private:
    friend struct AMessage;      // deliverMessage(),//AMessage作为友元类,AMessage可以访问AHandler中的私有成员。
    friend struct ALooperRoster; //

    ALooper::handler_id mID;//用于设置handler的id,以区分不同的handler
    wp<ALooper> mLooper;

    inline void setID(ALooper::handler_id id, const wp<ALooper> &looper) {
        mID = id;
        mLooper = looper;
    }

    bool mVerboseStats;//代表详细状态,主要用于dump信息时会用到
    uint32_t mMessageCounter;//表示消息的数量
    KeyedVector<uint32_t, uint32_t> mMessages;

    void deliverMessage(const sp<AMessage> &msg);//用于发送消息

    DISALLOW_EVIL_CONSTRUCTORS(AHandler);
};

2.2 AHandler::deliverMessage

1.从本系列的前面两篇,我们也应该十分清楚,最终looper会调用到deliverMessage方法,从而回到继承自AHandler子类我们重写的onMessageReceived中。

void AHandler::deliverMessage(const sp<AMessage> &msg) {
    onMessageReceived(msg);//此时便回到了继承Ahndler的子类的Ahandler中
    mMessageCounter++;//message数量+1

    if (mVerboseStats) {//默认mVerboseStats值为false,代表详细状态,主要用于dump信息时会用到
        uint32_t what = msg->what();
        ssize_t idx = mMessages.indexOfKey(what);
        if (idx < 0) {
            mMessages.add(what, 1);
        } else {
            mMessages.editValueAt(idx)++;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值