来写个demo试试看到底是ALooper AHandler AMessage是怎么运行的,源文件以及Android.bp如下:

// EvenHandler.h
#ifndef __MESSAGE_TEST_H__
#define __MESSAGE_TEST_H__ #include <media/stagefright/foundation/AMessage.h>
#include <media/stagefright/foundation/AHandler.h>
#include <media/stagefright/foundation/ALooper.h>
#include <media/stagefright/foundation/ADebug.h> namespace android{ class EventHandler : public AHandler
{
public:
EventHandler(); enum {
kWhatSetDataSource,
kWhatPrepare,
kWhatStart,
kWhatStop
}; void setDataSource(const char* url);
void prepare();
void start();
void stop(); protected:
virtual void onMessageReceived(const sp<AMessage> &msg); private:
void onSetDataSource(const char* url);
void onPrepare();
void onStart();
void onStop(); }; }
#endif
// EventHandler.cpp
#define LOG_TAG "MyEventHandler" #include "EventHandler.h"
#include <string.h> namespace android{ EventHandler::EventHandler()
{
ALOGD("tid = %d, func = %s", gettid(), __func__);
} void EventHandler::onMessageReceived(const sp<AMessage> &msg)
{
ALOGD("(%d)oMessageReceived: %s", gettid() ,msg->debugString().c_str());
switch(msg->what()){
case kWhatSetDataSource:
{
AString url;
CHECK(msg->findString("url", &url));
onSetDataSource(url.c_str());
break;
}
case kWhatPrepare:
{
sp<AReplyToken> replyID;
CHECK(msg->senderAwaitsResponse(&replyID));
onPrepare();
sp<AMessage> response = new AMessage;
response->setInt32("err", 0);
response->postReply(replyID);
break;
}
case kWhatStart:
{
onStart();
break;
}
case kWhatStop:
{
onStop();
break;
}
default:
break;
}
} void EventHandler::setDataSource(const char* url)
{
ALOGD("tid = %d, func = %s", gettid(), __func__);
sp<AMessage> msg = new AMessage(kWhatSetDataSource, this);
msg->setString("url", url, strlen(url));
msg->post();
} void EventHandler::prepare()
{
ALOGD("tid = %d, func = %s", gettid(), __func__);
sp<AMessage> msg = new AMessage(kWhatPrepare, this);
sp<AMessage> response;
msg->postAndAwaitResponse(&response);
int err;
CHECK(response->findInt32("err", &err));
ALOGD("tid = %d, func = %s, err = %d", gettid(), __func__, err);
} void EventHandler::start()
{
ALOGD("tid = %d, func = %s", gettid(), __func__);
sp<AMessage> msg = new AMessage(kWhatStart, this);
msg->post();
} void EventHandler::stop()
{
ALOGD("tid = %d, func = %s", gettid(), __func__);
sp<AMessage> msg = new AMessage(kWhatStop, this);
msg->post();
} void EventHandler::onSetDataSource(const char *url)
{
ALOGD("tid = %d, func = %s, url = %s", gettid(), __func__, url);
} void EventHandler::onPrepare()
{
ALOGD("tid = %d, func = %s", gettid(), __func__);
int cnt = 0;
while(cnt < 3)
{
ALOGD("tid = %d, func = %s, cnt = %d", gettid(), __func__, cnt);
sleep(1);
cnt++;
}
} void EventHandler::onStart()
{
ALOGD("tid = %d, func = %s", gettid(), __func__);
int cnt = 0;
while(cnt < 10)
{
ALOGD("tid = %d, func = %s, cnt = %d", gettid(), __func__, cnt);
sleep(1);
cnt++;
} } void EventHandler::onStop()
{
ALOGD("tid = %d, func = %s", gettid(), __func__);
} }
// main.cpp
#define LOG_TAG "MyEventHandlerTest" #include "EventHandler.h" using namespace android; int main(int argc, char **argv)
{
ALOGD("tid = %d, func = %s", gettid(), __func__);
sp<EventHandler> handler = new EventHandler; sp<ALooper> looper = new ALooper;
looper->setName("MyLooper");
looper->start();
looper->registerHandler(handler); handler->setDataSource("ABC");
handler->prepare();
handler->start();
handler->stop(); int wait = 0;
while(wait < 20)
{
ALOGD("wait = %d", wait);
sleep(1);
wait++;
}
}
// Android.bp
cc_binary {
name: "EventHandlerTest", srcs: [
"EventHandler.cpp",
"main.cpp"
], shared_libs: [
"libstagefright_foundation",
"libutils",
"liblog"
], include_dirs: [
"frameworks/av/media/libstagefright/foundation/include"
], cflags: [
"-Wno-unused-parameter"
] }

来看执行结果吧,试验了两种情况:

1、先说失败的,将looper->start(true),这个时候好像直接卡住不动了,

2、直接调用looper->start(),这里的参数默认为false,也就是会在一个新的线程当中做消息处理。从log中来看onMessageReceived方法都是在子线程中完成,在非阻塞的情况下是和主线程并行执行的。

这边还有个现象,main函数最后没有加while循环等待时,子线程执行完onStart方法进程就结束了,并没有等待处理完stop消息

03-25 17:08:23.437 16962 16962 D MyEventHandlerTest: tid = 16962, func = main
03-25 17:08:23.437 16962 16962 D MyEventHandler: tid = 16962, func = EventHandler
03-25 17:08:23.437 16962 16962 D MyEventHandler: tid = 16962, func = setDataSource
03-25 17:08:23.438 16962 16962 D MyEventHandler: tid = 16962, func = prepare
03-25 17:08:23.438 16962 16963 D MyEventHandler: (16963)oMessageReceived: AMessage(what = 0x00000000, target = 1) = {
03-25 17:08:23.438 16962 16963 D MyEventHandler: string url = "ABC"
03-25 17:08:23.438 16962 16963 D MyEventHandler: }
03-25 17:08:23.438 16962 16963 D MyEventHandler: tid = 16963, func = onSetDataSource, url = ABC
03-25 17:08:23.438 16962 16963 D MyEventHandler: (16963)oMessageReceived: AMessage(what = 0x00000001, target = 1) = {
03-25 17:08:23.438 16962 16963 D MyEventHandler: RefBase *replyID = 0xf5b01180
03-25 17:08:23.438 16962 16963 D MyEventHandler: }
03-25 17:08:23.438 16962 16963 D MyEventHandler: tid = 16963, func = onPrepare
03-25 17:08:23.438 16962 16963 D MyEventHandler: tid = 16963, func = onPrepare, cnt = 0
03-25 17:08:24.438 16962 16963 D MyEventHandler: tid = 16963, func = onPrepare, cnt = 1
03-25 17:08:25.438 16962 16963 D MyEventHandler: tid = 16963, func = onPrepare, cnt = 2
03-25 17:08:26.438 16962 16962 D MyEventHandler: tid = 16962, func = prepare, err = 0
03-25 17:08:26.439 16962 16962 D MyEventHandler: tid = 16962, func = start
03-25 17:08:26.439 16962 16962 D MyEventHandler: tid = 16962, func = stop
03-25 17:08:26.439 16962 16962 D MyEventHandlerTest: wait = 0
03-25 17:08:26.439 16962 16963 D MyEventHandler: (16963)oMessageReceived: AMessage(what = 0x00000002, target = 1) = {
03-25 17:08:26.439 16962 16963 D MyEventHandler: }
03-25 17:08:26.439 16962 16963 D MyEventHandler: tid = 16963, func = onStart
03-25 17:08:26.439 16962 16963 D MyEventHandler: tid = 16963, func = onStart, cnt = 0
03-25 17:08:27.439 16962 16962 D MyEventHandlerTest: wait = 1
03-25 17:08:27.439 16962 16963 D MyEventHandler: tid = 16963, func = onStart, cnt = 1
03-25 17:08:28.439 16962 16962 D MyEventHandlerTest: wait = 2
03-25 17:08:28.439 16962 16963 D MyEventHandler: tid = 16963, func = onStart, cnt = 2
03-25 17:08:29.439 16962 16963 D MyEventHandler: tid = 16963, func = onStart, cnt = 3
03-25 17:08:29.439 16962 16962 D MyEventHandlerTest: wait = 3
03-25 17:08:30.439 16962 16963 D MyEventHandler: tid = 16963, func = onStart, cnt = 4
03-25 17:08:30.439 16962 16962 D MyEventHandlerTest: wait = 4
03-25 17:08:31.439 16962 16963 D MyEventHandler: tid = 16963, func = onStart, cnt = 5
03-25 17:08:31.440 16962 16962 D MyEventHandlerTest: wait = 5
03-25 17:08:32.439 16962 16963 D MyEventHandler: tid = 16963, func = onStart, cnt = 6
03-25 17:08:32.440 16962 16962 D MyEventHandlerTest: wait = 6
03-25 17:08:33.440 16962 16963 D MyEventHandler: tid = 16963, func = onStart, cnt = 7
03-25 17:08:33.440 16962 16962 D MyEventHandlerTest: wait = 7
03-25 17:08:34.440 16962 16963 D MyEventHandler: tid = 16963, func = onStart, cnt = 8
03-25 17:08:34.440 16962 16962 D MyEventHandlerTest: wait = 8
03-25 17:08:35.440 16962 16963 D MyEventHandler: tid = 16963, func = onStart, cnt = 9
03-25 17:08:35.440 16962 16962 D MyEventHandlerTest: wait = 9
03-25 17:08:36.440 16962 16963 D MyEventHandler: (16963)oMessageReceived: AMessage(what = 0x00000003, target = 1) = {
03-25 17:08:36.440 16962 16963 D MyEventHandler: }
03-25 17:08:36.440 16962 16963 D MyEventHandler: tid = 16963, func = onStop
03-25 17:08:36.441 16962 16962 D MyEventHandlerTest: wait = 10
03-25 17:08:37.441 16962 16962 D MyEventHandlerTest: wait = 11
03-25 17:08:38.441 16962 16962 D MyEventHandlerTest: wait = 12
03-25 17:08:39.441 16962 16962 D MyEventHandlerTest: wait = 13
03-25 17:08:40.441 16962 16962 D MyEventHandlerTest: wait = 14
03-25 17:08:41.441 16962 16962 D MyEventHandlerTest: wait = 15
03-25 17:08:42.441 16962 16962 D MyEventHandlerTest: wait = 16
03-25 17:08:43.442 16962 16962 D MyEventHandlerTest: wait = 17
03-25 17:08:44.442 16962 16962 D MyEventHandlerTest: wait = 18
03-25 17:08:45.442 16962 16962 D MyEventHandlerTest: wait = 19

Android 12(S) ALooper AHandler AMessage(二)的更多相关文章

  1. Android 12(S) 图形显示系统 - Surface 一点补充知识(十二)

    必读: Android 12(S) 图形显示系统 - 开篇 一.前言 因为个人工作主要是Android多媒体播放的内容,在工作中查看源码或设计程序经常会遇到调用API: static inline i ...

  2. Android 12(S) 图形显示系统 - 示例应用(二)

    1 前言 为了更深刻的理解Android图形系统抽象的概念和BufferQueue的工作机制,这篇文章我们将从Native Level入手,基于Android图形系统API写作一个简单的图形处理小程序 ...

  3. Android应用--QR的生成(二维码)

    二维码的定义: 二维码(2-dimensional bar code),是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的. 在许多种类的二维条码中,常用的码制 ...

  4. Android基础——项目的文件结构(二)

    Android基础--项目的文件结构(二) AndroidManifest.xml文件分析 [注]此项目文件结构仅限于Android Studio下的Android项目!!! 在一个Android项目 ...

  5. Android 12(S) 图形显示系统 - createSurface的流程(五)

    题外话 刚刚开始着笔写作这篇文章时,正好看电视在采访一位92岁的考古学家,在他的日记中有这样一句话,写在这里与君共勉"不要等待幸运的降临,要去努力的掌握知识".如此朴实的一句话,此 ...

  6. Android 12(S) 图形显示系统 - BufferQueue/BLASTBufferQueue之初识(六)

    题外话 你有没有听见,心里有一声咆哮,那一声咆哮,它好像在说:我就是要从后面追上去! 写文章真的好痛苦,特别是自己对这方面的知识也一知半解就更加痛苦了.这已经是这个系列的第六篇了,很多次都想放弃了,但 ...

  7. Android 12(S) 图形显示系统 - 初识ANativeWindow/Surface/SurfaceControl(七)

    题外话 "行百里者半九十",是说步行一百里路,走过九十里,只能算是走了一半.因为步行越接近目的地,走起来越困难.借指凡事到了接近成功,往往是最吃力.最艰难的时段.劝人做事贵在坚持, ...

  8. Android 12(S) 图形显示系统 - BufferQueue的工作流程(八)

    题外话 最近总有一个感觉:在不断学习中,越发的感觉自己的无知,自己是不是要从"愚昧之巅"掉到"绝望之谷"了,哈哈哈 邓宁-克鲁格效应 一.前言 前面的文章中已经 ...

  9. Android 12(S) 图形显示系统 - BufferQueue的工作流程(九)

    题外话 Covid-19疫情的强烈反弹,小区里检测出了无症状感染者.小区封闭管理,我也不得不居家办公了.既然这么大把的时间可以光明正大的宅家里,自然要好好利用,八个字 == 努力工作,好好学习 一.前 ...

  10. Android 12(S) 图形显示系统 - BufferQueue的工作流程(十)

    题外话 疫情隔离在家,周末还在努力学习的我  ..... 一.前言 上一篇文章中,有基本讲清楚Producer一端的处理逻辑,最后也留下了一个疑问: Consumer是什么时候来消费数据的?他是自己主 ...

随机推荐

  1. Avalonia下拉可搜索树(TreeComboBox)

    1.需求分析   树形下拉的功能是ComboBox和TreeView的功能结合起来,再结合数据模板来实现这一功能. 2.代码实现   1.创建UserControl集成TreeView控件   2.将 ...

  2. 本周三晚19:00 Hello HarmonyOS进阶课程第6课—短视频应用开发

    短视频应用软件的开发一直保持着快速发展,在用户流量增长和规模扩大的同时,短视频行业的受欢迎程度也在持续上升.在生活节奏不断加快的今天,人们过着越来越充实的生活,碎片化已经渐渐成为人们习以为常的节奏,比 ...

  3. iOS系统崩溃的捕获

    iOS系统崩溃的捕获 相信大家在开发iOS程序的时候肯定写过各种Bug,而其中最为严重的Bug就是会导致崩溃的Bug(一般来说妥妥的P1级).在应用软件大大小小的各种异常中,崩溃确实是最让人难以接受的 ...

  4. Top 20 OpenSSH Server Best Security Practices

    Top 20 OpenSSH Server Best Security Practices by NIX Craft on July 24, 2009 · 139 comments· LAST UPD ...

  5. 国产gowin开发板GW1NR-9K的PSRAM使用说明

    开发板子采用GW1NNR-LV9LQ144PC6/I5 FPGA器件.具有低功耗,瞬时启动,高安全性,低成本,方便扩展等特点.本开发板价格价格便宜,板子扩张性容易,帮助用户比较快速进入国产FPGA学习 ...

  6. HL7消息类型

    HL7消息有很多不同的类型,每种都有其自己的独特用途和消息内容.以下是常见的HL7消息类型的列表. Message Type Description HL7 ADT Admit, Discharge ...

  7. HarmonyOS NEXT应用开发案例—状态栏显隐变化

    介绍 本示例介绍使用Scroll组件的滚动事件 onScroll 实现状态栏显隐变化.该场景多用于各种软件的首页.我的等页面中. 效果预览图 使用说明 加载完成后显示状态栏显隐变化页面,上下拖动屏幕, ...

  8. 通过部署流行Web框架掌握Serverless技术

    简介: 通过学习部署流行Web框架,如SpringBoot,Express,Web IDE,让你掌握Serverless函数计算架构和技术,领略弹性并发,高可用的好处.大家好,我是霍大侠,欢迎来到我的 ...

  9. 流批一体生产应用!Bigo 实时计算平台建设实践

    简介: 本文由 Bigo 计算平台负责人徐帅分享,主要介绍 Bigo 实时计算平台建设实践的介绍 本文由 Bigo 计算平台负责人徐帅分享,主要介绍 Bigo 实时计算平台建设实践的介绍.内容包括: ...

  10. 消息队列 RocketMQ 遇上可观测:业务核心链路可视化

    ​简介:本篇文章主要介绍 RocketMQ 的可观测性工具在线上生产环境的最佳实践.RocketMQ的可观测性能力领先业界同类产品,RocketMQ 的 Dashboard 和消息轨迹等功能为业务核心 ...