android binder 机制三(匿名Service)
什么是匿名Service?凡是没有到ServiceManager上注冊的Service,都是匿名Service。
还是拿上一篇的样例来举例,看代码:
- status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
- {
- status_t err = UNKNOWN_ERROR;
- const sp<IMediaPlayerService>& service(getMediaPlayerService());
- if (service != 0) {
- sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
- if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
- (NO_ERROR != player->setDataSource(fd, offset, length))) {
- player.clear();
- }
- err = attachNewPlayer(player);
- }
- return err;
- }
在BpMediaPlayerService中。create的实现例如以下:
- virtual sp<IMediaPlayer> create(
- const sp<IMediaPlayerClient>& client, int audioSessionId) {
- Parcel data, reply;
- data.writeInterfaceToken(IMediaPlayerService::getInterfaceDescriptor());
- data.writeStrongBinder(client->asBinder());
- data.writeInt32(audioSessionId);
- remote()->transact(CREATE, data, &reply);
- return interface_cast<IMediaPlayer>(reply.readStrongBinder());
- }
直接跳到服务端MediaPlayerService,看create的真正实现:
- sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
- int audioSessionId)
- {
- pid_t pid = IPCThreadState::self()->getCallingPid();
- int32_t connId = android_atomic_inc(&mNextConnId);
- sp<Client> c = new Client(
- this, pid, connId, client, audioSessionId,
- IPCThreadState::self()->getCallingUid());
- ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
- IPCThreadState::self()->getCallingUid());
- /* add by Gary. start {{----------------------------------- */
- c->setScreen(mScreen);
- /* add by Gary. end -----------------------------------}} */
- c->setSubGate(mGlobalSubGate); // 2012-03-12, add the global interfaces to control the subtitle gate
- wp<Client> w = c;
- {
- Mutex::Autolock lock(mLock);
- mClients.add(w);
- }
- return c;
- }
从代码中,我们能够看出,service->create(this, mAudioSessionId)是返回了一个參数为Client类型的BpMediaPlayer对象,当中Client为MediaPlayerService的私有内部类,其声明为:class Client : publicBnMediaPlayer。
这样,Binder通信的服务端和client就建立起来了。Client端BpMediaPlayer由MediaPlayer使用,Server端BnMediaPlayer由MediaPlayerService使用。
BpMediaPlayer是怎样获得BnMediaPlayer的handle值的呢?答案在MediaPlayerService返回这个binder的引用的时候,binder驱动保存了这个binder实体的各种数据。创建了节点,看以下的代码:
- status_t BnMediaPlayerService::onTransact(
- uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
- {
- switch (code) {
- case CREATE: {
- CHECK_INTERFACE(IMediaPlayerService, data, reply);
- sp<IMediaPlayerClient> client =
- interface_cast<IMediaPlayerClient>(data.readStrongBinder());
- int audioSessionId = data.readInt32();
- sp<IMediaPlayer> player = create(client, audioSessionId);
- reply->writeStrongBinder(player->asBinder());
- return NO_ERROR;
- } break;
- ……
- }
- }
答案就在这句话中:reply->writeStrongBinder(player->asBinder());
当这个reply写到Binder驱动时,驱动会特殊处理这样的IBinder类型的数据,为Bbinder建立一个handle。
通信的通路建立后,就能够进行通信了:player->setDataSource(fd, offset, length)
之后的实现,和上一篇讲的普通Service就一样了。
android binder 机制三(匿名Service)的更多相关文章
- 【转】Android - Binder机制
以下几篇文章是分析binder机制里讲得还算清楚的 目录 1. Android - Binder机制 - ServiceManager 2. Android - Binder机制 - 普通servic ...
- Android Binder机制简单了解
Binder -- 一种进程间通信(IPC)机制, 基于OpenBinder来实现 毫无疑问, 老罗的文章是不得不看的 Android进程间通信(IPC)机制Binder简要介绍和学习计划 浅谈Ser ...
- Android Binder机制彻底梳理二
根据AIDL了解整体调用流程[重点分析AIDL流程]: 在上一次https://www.cnblogs.com/webor2006/p/11741743.html中我们已经对Android Binde ...
- Android Binder机制原理(史上最强理解,没有之一)(转)
原文地址: http://blog.csdn.net/universus/article/details/6211589 Binder是Android系统进程间通信(IPC)方式之一.Linux已经拥 ...
- Android Binder机制详解:手写IPC通信
想要掌握一样东西,最好的方式就是阅读理解它的源码.想要掌握Android Binder,最好的方式就是写一个AIDL文件,然后查看其生成的代码.本文的思路也是来自于此. 简介 Binder是Andro ...
- ANDROID BINDER机制浅析
Binder是Android上一种IPC机制,重要且较难理解.由于Linux上标准IPC在灵活和可靠性存在一定不足,Google基于OpenBinder的设计和构想实现了Binder. 本文只简单介绍 ...
- Android Binder机制彻底梳理一
Binder架构图: 先来瞅一下它的整体架构图: 其中粉红部分是上层的Binder,而蓝色的则是下层的Binder,很显然上层的是依赖于下层的. 什么是Binder[有个大概了解]? 这里从几个层面来 ...
- Android binder机制---概述
1.进程间通讯的原因 目前操作系统都使用虚拟存储技术,管理内存. 假设是32位机器,0-3G是用户空间,3-4G是系统使用.虚拟内存和逻辑内存都按4K分页.这样虚拟内存和逻辑内存就存在对应关系. 一个 ...
- android binder机制详解
摘要 Binder是android中一个很重要且很复杂的概念,它在系统的整体运作中发挥着极其重要的作用,不过本文并不打算从深层次分析Binder机制,有两点原因:1是目前网上已经有2篇很好的文章了,2 ...
随机推荐
- nw335 debian sid x86-64 -- 6 第三方驱动
nw335 debian sid x86-64 -- 6 第三方驱动
- Android AAR 混淆的坑
一定不要忘记加上这段 -keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,A ...
- Html + Css 小知识点
选择器 根据选择器来对html内的内容做css修饰 样式: 找到一个元素{ 样式:值: } 找到一个元素:选择器 css都在style标签内部写 1.标签选择器: 根据标签名查找. 小丽: 2.id选 ...
- 爬虫开发python工具包介绍 (4)
本文来自网易云社区 作者:王涛 此处我们给出几个常用的代码例子,包括get,post(json,表单),带证书访问:Get 请求 @gen.coroutine def fetch_url(): ...
- unittest编写Web测试用例
案例:百度搜索关键词:“unittest” test_baidu.py: from selenium import webdriver from time import sleep import un ...
- Python第三方库之openpyxl(2)
Python第三方库之openpyxl(2) 简单的使用 写一个工作簿 >>> from openpyxl import Workbook >>> from ope ...
- HDU-3743 Minimum Sum,划分树模板
Minimum Sum 被这个题坑了一下午,原来只需找一个最中间的数即可,我以为是平均数. 题意:找一个数使得这个数和区间内所有数的差的绝对值最小.输出最小值. 开始用线段树来了一发果断T了,然后各种 ...
- 【bzoj1444】[Jsoi2009]有趣的游戏 AC自动机+矩阵乘法
题目描述 输入 注意 是0<=P 输出 样例输入 样例输出 题解 AC自动机+矩阵乘法 先将所有字符串放到AC自动机中,求出Trie图. 然后构建邻接矩阵:如果x不是某个字符串的末位置,则x连向 ...
- ZipKin原理学习--zipkin支持日志打印追踪信息
目前在zipkin brave已经提供功能在我们使用logback或log4j等时可以在日志信息中将traceId和spanId等信息打印到运行日志,这样可能对我们通过日志查看解决问题有比较大的 ...
- 算法复习——floyd求最小环(poj1734)
题目: 题目描述 N 个景区,任意两个景区之间有一条或多条双向的路来连接,现在 Mr.Zeng 想找一条旅游路线,这个路线从A点出发并且最后回到 A 点,假设经过的路线为 V1,V2,....VK,V ...