AHandler AMessage ALooper消息机制-源码6.0.0以上
AHandler AMessage ALooper消息机制
AHandler
是Android native层实现的一个异步消息机制,在这个机制中所有的处理都是异步的,将变量封装到一个消息AMessage
结构体中,然后放到队列中去,后台专门有一个线程ALooper
会从这个队列中取出消息然后分发执行,执行函数就是AHandler实例的onMessageReceived
。
注意:本文中源码是基于Android11的,6.0以上都是相同的,6.0以下的源码略有不同
1.主要类
主要的涉及的有以下三个类
AMessage
消息类,用于构造消息,通过post方法投递出去给ALooperALooper
轮询类,用于保存消息队列,然后将消息发送到对应的AHandlerAHandler
处理类,用于实际消息的处理
2.消息类型
发送的消息按照是否需要答复,分为两种
- 普通消息
- 需答复消息
3.模型简介
普通消息被Post
到消息队列,Looper
不断的从队列中取出消息并分发给相应的Handler
处理。如果是需答复消息的话使用PostAndAwaitResponse
,Handler
处理完以后通过postReply
将消息返回给发送方,发送方通过新建的responsemsg
接收。
4.代码分析
4.1 初始化
伪代码
sp<ALooper> looper = new ALooper;//创建一个ALooper实例
sp<AHandlerReflector> mHandler = new AHandlerReflector();//创建一个AHandler实例
looper->registerHandler(mHandler);//1. registerHandler会调用AHandler的setID方法将looper设置到Handler里面
looper->start(true);//2. 根据参数创建并启动LooperThread
代码追踪
- 注册mHandler到looper
ALooper.cpp
ALooper::handler_id ALooper::registerHandler(const sp<AHandler> &handler) {
return gLooperRoster.registerHandler(this, handler);
}
直接调用ALooperRoster
的registerHandler
,生成一个handler ID
并保存起来
ALooperRoster.cpp
ALooper::handler_id ALooperRoster::registerHandler(
const sp<ALooper> &looper, const sp<AHandler> &handler) {
Mutex::Autolock autoLock(mLock);
if (handler->id() != 0) {
CHECK(!"A handler must only be registered once."); //一个handler在同一个looper中只能注册一次
return INVALID_OPERATION;
}
HandlerInfo info; //HandlerInfo 结构体,保存looper和handler,将其绑定在一起
info.mLooper = looper;
info.mHandler = handler;
ALooper::handler_id handlerID = mNextHandlerID++; //生成handler ID
mHandlers.add(handlerID, info); //mHandlers为ALooperRoster的私有,保存handler列表
handler->setID(handlerID, looper);
return handlerID;
}
- 启动looper循环
ALooper.cpp
status_t ALooper::start(
bool runOnCallingThread, bool canCallJava, int32_t priority) {
if (runOnCallingThread) {
{
Mutex::Autolock autoLock(mLock);
if (mThread != NULL || mRunningLocally) {
return INVALID_OPERATION;
}
mRunningLocally = true;
}
do {
} while (loop());
return OK;
}
Mutex::Autolock autoLock(mLock);
if (mThread != NULL || mRunningLocally) {
return INVALID_OPERATION;
}
mThread = new LooperThread(this, canCallJava);//开启一个线程
status_t err = mThread->run(
mName.empty() ? "ALooper" : mName.c_str(), priority);
if (err != OK) {
mThread.clear();
}
return err;
}
4.2 普通消息使用流程
- 实现一个
AHandler
的子类,主要是onMessageReceived
方法 - 创建一个
ALooper
对象 - 创建消息
AMessage
进行投递Post
onMessageReceived
方法得到调用,处理实际的事务
伪代码
发送
sp<AMessage> msg = new AMessage(kWhatSaySomething, mHandler); //在AMessage的构造方法里获取Ahandler对应的Looper并保存
msg->post(); //1. 发送消息
处理
AHandlerReflector:: onMessageReceived //消息的具体的处理实现
代码追踪
发送消息
AMessage.cpp
status_t AMessage::post(int64_t delayUs) {
sp<ALooper> looper = mLooper.promote();
if (looper == NULL) {
ALOGW("failed to post message as target looper for handler %d is gone.", mTarget);
return -ENOENT;
}
looper->post(this, delayUs); //进到looper线程
return OK;
}
ALooper.cpp
void ALooper::post(const sp<AMessage> &msg, int64_t delayUs) {
Mutex::Autolock autoLock(mLock);
int64_t whenUs;
if (delayUs > 0) {
whenUs = GetNowUs() + delayUs;
} else {
whenUs = GetNowUs();
}
List<Event>::iterator it = mEventQueue.begin();
while (it != mEventQueue.end() && (*it).mWhenUs <= whenUs) {
++it;
}
Event event;
event.mWhenUs = whenUs;
event.mMessage = msg;
if (it == mEventQueue.begin()) {
mQueueChangedCondition.signal(); //如果之前消息队列为空,没有event,loop是处于阻塞状态的,此处当有消息进入时,先通知looperthread启动
}
mEventQueue.insert(it, event); //进入消息队列
}
looper循环进行分发
ALooper.cpp
bool ALooper::loop() {
Event event;
{
Mutex::Autolock autoLock(mLock);
if (mThread == NULL && !mRunningLocally) {
return false;
}
if (mEventQueue.empty()) {
mQueueChangedCondition.wait(mLock); //如果消息队列为空,则等待,进入阻塞状态
return true;
}
int64_t whenUs = (*mEventQueue.begin()).mWhenUs;
int64_t nowUs = GetNowUs();
if (whenUs > nowUs) {
int64_t delayUs = whenUs - nowUs;
mQueueChangedCondition.waitRelative(mLock, delayUs * 1000ll);
return true;
}
event = *mEventQueue.begin(); //取出队列排头消息
mEventQueue.erase(mEventQueue.begin()); //退出消息队列
}
event.mMessage->deliver(); //根据每个消息携带的handler做转发处理
// NOTE: It's important to note that at this point our "ALooper" object
// may no longer exist (its final reference may have gone away while
// delivering the message). We have made sure, however, that loop()
// won't be called again.
return true;
}
返回工作线程
AMessage.cpp
void AMessage::deliver() {
sp<AHandler> handler = mHandler.promote();
if (handler == NULL) {
ALOGW("failed to deliver message as target handler %d is gone.", mTarget);
return;
}
handler->deliverMessage(this); //转发到相应的handler实例
}
转发到相应的Handler
void AHandler::deliverMessage(const sp<AMessage> &msg) {
onMessageReceived(msg);//Handler实例必须覆盖此方法,在这里做对应的处理,一般采用switch语句进行不同的处理
mMessageCounter++;
if (mVerboseStats) {
uint32_t what = msg->what();
ssize_t idx = mMessages.indexOfKey(what);
if (idx < 0) {
mMessages.add(what, 1);
} else {
mMessages.editValueAt(idx)++;
}
}
}
4.3 需要答复的消息使用流程
- 实现一个
AHandler
的子类,主要是onMessageReceived
方法 - 创建一个
ALooper
对象 - 创建消息
AMessage
进行投递PostAndAwaitResponse
onMessageReceived
方法得到调用,处理实际的事务- 处理完消息后发送答复
postReply
- 发送端接收答复消息
response
伪代码
发送/接收答复
sp<AMessage> msg = new AMessage(kWhatSaySomething, mHandler); //发送的消息
sp<AMessage> responsemsg; //接收存储答复消息
msg->postAndAwaitResponse(responsemsg);
处理/答复
AHandlerReflector:: onMessageReceived //消息的具体的处理实现
sp<AReplyToken> replyID;
CHECK(msg->senderAwaitsResponse(&replyID));
sp<AMessage> response = new AMessage;//创建答复消息
response->postReply(replyID);//发送答复消息
代码追踪
AMessage.cpp
status_t AMessage::postAndAwaitResponse(sp<AMessage> *response) {
return gLooperRoster.postAndAwaitResponse(this, response);
}
AMessage.cpp
status_t AMessage::postAndAwaitResponse(sp<AMessage> *response) {
sp<ALooper> looper = mLooper.promote();
if (looper == NULL) {
ALOGW("failed to post message as target looper for handler %d is gone.", mTarget);
return -ENOENT;
}
sp<AReplyToken> token = looper->createReplyToken(); 创建Token标识,此token用来唯一的标识这条信息
if (token == NULL) {
ALOGE("failed to create reply token");
return -ENOMEM;
}
setObject("replyID", token); //将此token添加到该Message的replyID字段
looper->post(this, 0 /* delayUs */); //发给looper
return looper->awaitResponse(token, response); //在此处就阻塞住,不返回了
}
post
过程和普通消息的一致,先发送到looper
,然后loop
分发给对应的handler
处理,不同的是此处在mRepliesCondition.wait(mRepliesLock)
阻塞等待
ALooper.cpp
status_t ALooper::awaitResponse(const sp<AReplyToken> &replyToken, sp<AMessage> *response) {
// return status in case we want to handle an interrupted wait
Mutex::Autolock autoLock(mRepliesLock);
CHECK(replyToken != NULL);
while (!replyToken->retrieveReply(response)) { //第一次会进入这个循环
{
Mutex::Autolock autoLock(mLock);
if (mThread == NULL) {
return -ENOENT;
}
}
mRepliesCondition.wait(mRepliesLock); //等待直到收到答复广播
}
return OK;
}
等收到mRepliesCondition.broadcast()
以后,会执行retrieveReply
,它的作用是将返回的reply
拷贝到response
AReplyToken
是一个结构体,定义在AMessage.h
文件中
// if reply is not set, returns false; otherwise, it retrieves the reply and returns true
bool retrieveReply(sp<AMessage> *reply) {
if (mReplied) {
*reply = mReply;
mReply.clear();
}
return mReplied;
}
所以上面的程序就在等待mRepliesCondition.broadcast()
的发出,而这个broadcast
是在哪里发出了,我们接着看
接着看消息的处理,和普通消息一致,在相应的handler
实例的onMessageReceived
中得到处理,处理完以后创建答复消息
sp<AReplyToken> replyID;
CHECK(msg->senderAwaitsResponse(&replyID));
sp<AMessage> response = new AMessage;//创建答复消息
response->postReply(replyID);//发送答复消息
答复消息也是也AMessage
的形式发出
AMessage.cpp
status_t AMessage::postReply(const sp<AReplyToken> &replyToken) {
if (replyToken == NULL) {
ALOGW("failed to post reply to a NULL token");
return -ENOENT;
}
sp<ALooper> looper = replyToken->getLooper();//根据replyToken获取消息所在的looper
if (looper == NULL) {
ALOGW("failed to post reply as target looper is gone.");
return -ENOENT;
}
return looper->postReply(replyToken, this); //转到looper
}
在looper
这里首先将reply
消息拷贝的位于AReplyToken
的一个缓冲变量mReply
,然后发出回复成果广播
ALooper.cpp
status_t ALooper::postReply(const sp<AReplyToken> &replyToken, const sp<AMessage> &reply) {
Mutex::Autolock autoLock(mRepliesLock);
status_t err = replyToken->setReply(reply); //将回复消息保存
if (err == OK) {
mRepliesCondition.broadcast(); /发出已回复广播
}
return err;
}
保存回复信息到缓存变量mReply
AMessage.cpp
status_t AReplyToken::setReply(const sp<AMessage> &reply) {
if (mReplied) {
ALOGE("trying to post a duplicate reply");
return -EBUSY;
}
CHECK(mReply == NULL);
mReply = reply; //保存回复信息
mReplied = true;
return OK;
}
参考:
AHandler AMessage ALooper消息机制
AHandler AMessage ALooper消息机制-源码6.0.0以上的更多相关文章
- Android Handler消息机制源码解析
好记性不如烂笔头,今天来分析一下Handler的源码实现 Handler机制是Android系统的基础,是多线程之间切换的基础.下面我们分析一下Handler的源码实现. Handler消息机制有4个 ...
- Android消息机制源码分析
本篇主要介绍Android中的消息机制,即Looper.Handler是如何协同工作的: Looper:主要用来管理当前线程的消息队列,每个线程只能有一个Looper Handler:用来将消息(Me ...
- [Android]简略的Android消息机制源码分析
相关源码 framework/base/core/java/andorid/os/Handler.java framework/base/core/java/andorid/os/Looper.jav ...
- 史上最详细的Android消息机制源码解析
本人只是Android菜鸡一个,写技术文章只是为了总结自己最近学习到的知识,从来不敢为人师,如果里面有不正确的地方请大家尽情指出,谢谢! 606页Android最新面试题含答案,有兴趣可以点击获取. ...
- Android事件分发机制源码分析
Android事件分发机制源码分析 Android事件分发机制源码分析 Part1事件来源以及传递顺序 Activity分发事件源码 PhoneWindow分发事件源码 小结 Part2ViewGro ...
- Springboot学习04-默认错误页面加载机制源码分析
Springboot学习04-默认错误页面加载机制源码分析 前沿 希望通过本文的学习,对错误页面的加载机制有这更神的理解 正文 1-Springboot错误页面展示 2-Springboot默认错误处 ...
- 阿里云视频直播API签名机制源码
阿里云视频直播API签名机制源码 本文展示:通过代码实现下阿里视频直播签名处理规则 阿里云视频直播签名机制,官方文档链接:https://help.aliyun.com/document_detail ...
- ApplicationEvent事件机制源码分析
<spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情> <服务网关zu ...
- 【转】Win 7 下源码运行OpenERP7.0
原文地址:Win 7 下源码运行OpenERP7.0 安装Python2.7 下载地址:http://www.python.org/getit/注:OpenERP7.0支持的Python版本最高为2. ...
- CentOS7源码安装Redis5.0.4非关系型数据库
源码安装redis-5.0.4 一. 下载redis 1. 需要连接网络 二. 案例(另一种安装方法) [root@localhost ~]# wget http://download.redis.i ...
随机推荐
- MarkDown文件插入公式(常用格式)
1.插入公式 markdown支持插入公式,书写公式需要按照特定格式来写,涉及到希腊字母.符号.角标.基本语法等内容需要熟悉, 1.1 句中插入公式 表达式前后插入$即可 ,比如$\alpha$,显示 ...
- 针对SpringBoot服务端返回的空对象和空数组问题
返回的Json会自动过滤掉空指针的对象,但是若遇到非空指针的没有任何内容的对象,举例如下: public class Person { private String name; private Int ...
- 【SpringBootStarter】自定义全局加解密组件
[SpringBootStarter] 目的 了解SpringBoot Starter相关概念以及开发流程 实现自定义SpringBoot Starter(全局加解密) 了解测试流程 优化 最终引用的 ...
- Unicode编码的魅力:跨语言交流的桥梁
引言: Unicode编码是一种用于表示世界上所有字符的标准编码方式.它解决了字符集兼容性和多语言文本处理的难题,成为实现全球化软件的关键技术.本文将深入探讨Unicode编码的优点与缺点,并介绍它在 ...
- 轻松玩转makefile|基础知识
1.什么是Makefile 在嵌入式软件开发领域,使用 make 工具构建开发环境是非常常见的.而要使用 make 工具,就需要编写 Makefile 文件来定义一系列规则,指定需要先编译.后编译 ...
- 【Android】使用AIDL实现进程间通讯简单案例
1 AIDL 简介 AIDL(Android Interface Definition Language)是一种接口定义语言,用于生成可在 Android 设备上两个进程之间进行进程间通信(IPC ...
- Js实现链表操作
Js实现链表操作 JavaScript实现链表主要操作,包括创建链表.遍历链表.获取链表长度.获取第i个元素值.获取倒数第i个元素值.插入节点.删除节点.有序链表合并.有序链表交集. 创建链表 cla ...
- js常用知识点整理
说明:以下内容都是我工作中实际碰到的js知识点. 后面还会碰到其他知识点或对原来解决方案的改进,都会在本篇中持续不断的维护,希望给刚参加工作或初学的朋友一些参考. 1.给元素添加事件 $(" ...
- 如何基于three.js(webgl)引擎架构,实现3D密集架库房,3D档案室(3d机器人取档、机器人盘点、人工查档、设备巡检)
前言: 这是最好的时代,也是最坏的时代:是充满挑战的时代,也是充满机遇的时代.是科技飞速的时代,也是无限可能的时代. 近年来,人工智能(AI)技术的飞速发展已经席卷了全球,不断突破着技术边界,为各行 ...
- Navicat安装包和激活工具
链接:https://pan.baidu.com/s/11YuOKdgGakt3E8CL80aA2g 提取码:h4f3