上一篇 Binder机制,从Java到C (2. IPC in System Service :AMS)  中提到 Application是通过ServiceManager找到了AMS 的service代理对象。那在这个之前当然是要先找到ServiceManager的代理对象,才能调用ServiceManager的服务嘛。下面就看看怎么来获得这个代理对象的吧:

还记得上一篇调用的ServiceManager的方法吧:IBinder b = ServiceManager.getService("activity");

那下面就來看一下ServiceManager.getService()是怎样的:
/frameworks/base/core/java/android/os/ServiceManager.java

 public final class ServiceManager {
...
private static IServiceManager getIServiceManager() {
if (sServiceManager != null) {
return sServiceManager;
} // Find the service manager
sServiceManager = ServiceManagerNative.asInterface(BinderInternal.getContextObject()); (1)
return sServiceManager;
} public static IBinder getService(String name) {
try {
IBinder service = sCache.get(name);
if (service != null) {
return service;
} else {
return getIServiceManager().getService(name);
}
} catch (RemoteException e) {
Log.e(TAG, "error in getService", e);
}
return null;
}

说明:

(1) :根据前两篇的分析,我们可以推測是在BinderInternal.getContextObject()中,找到了ServiceManager的BinderProxy,似乎和前面有些不同。接着看。

那就接着看BinderInternal.getContextObject()有什麼不同:
/framework/base/core/java/com/android/internal/os/BinderInternal.java

 public class BinderInternal {
….
public static final native IBinder getContextObject();
….
}

会getContextObject()是个JNI方法, ServiceManager难道运行在native环境?答案是Yes! ServiceManager确实运行在Native环境。

继续看,在/framework/base/core/jni/android_util_Binder.cpp找到了对应的方法:

 static jobject android_os_BinderInternal_getContextObject(JNIEnv* env, jobject clazz)
{
sp<IBinder> b = ProcessState::self()->getContextObject(NULL);
return javaObjectForIBinder(env, b); //按照字面意思来看,就是把一个Native层的Binder代理对象转化成Java层的Binder代理对象
}

这个方法只有两句代码,第一句看着就是找一个IBinder对象啦,然后第二句,就是把一个Native里面的Binder代理对象转化成Java层的Binder代理对象,然后好返回给Java层。

来看一下第一句代码,在/frameworks/native/libs/binder/ProcessState.cpp里:

 sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& caller)
{
return getStrongProxyForHandle(0);
}

里面的这一句代码就暂时先不展开了,不然没完没了了,這里按字面意思也可以看出,是根据一个handle來获得一个Native层的Binder代理对象。而是一個default值。代表servicemanager的binder handle值。所以这里你给个0,就返回给你servicemanager的Binder代理对象。

那到这里,我們就认为通过BinderInternal.getContextObject(),获得了Native的一個binder 代理对象后,javaObjectForIBinder()会创建一个BinderProxy对象返回到Java层去。
经过上面的分析,我们可以知道:ServiceManager的Stub端在Native层。ServiceManager的Proxy端在Java层

结合前面两篇总结一下:
1. Application通过AMS的static 方法,先得到ServiceManager的BinderProxy。
2. 然后从ServiceManager获得AMS的BinderProxy,从而可以使用AMS的远程服务方法。
3. Application通过AMS的远程服务方法,如bindservice(), 將Application Remote Service的BinderProxy提供给Activity,从而Activity可以远程调用这些remote service 的服务。

现在可以串起来了吧。

Binder机制,从Java到C (3. ServiceManager in Java)的更多相关文章

  1. Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6642463 在前面几篇文章中,我们详细介绍了A ...

  2. Binder机制,从Java到C (8. ServiceManager in Native)

    在第三篇 Binder机制,从Java到C (3. ServiceManager in Java) 中,讲到ServiceManager的Stub端在Native,Proxy端在Java.实际上,还要 ...

  3. Binder机制,从Java到C (大纲)

    转载请标注:张小燕:http://www.cnblogs.com/zhangxinyan/p/3487381.html 前段时间一直在看有关Binder机制的内容,觉得受益匪浅,整理记录于此,大家请随 ...

  4. Binder机制,从Java到C (2. IPC in System Service :AMS)

    1.建立Activity和Service的IPC之前 在上一篇 Binder机制,从Java到C (1. IPC in Application Remote Service)  里面有说到Activi ...

  5. Binder机制,从Java到C (1. IPC in Application Remote Service)

    转载请标注:张小燕:http://www.cnblogs.com/zhangxinyan 1. Application 中的 service 我们知道Android中Service有三种类型:Loca ...

  6. Binder机制,从Java到C (5. IBinder对象传递形式)

    1.IBinder的传递 Binder IPC通信中,Binder是通信的媒介,Parcel是通信的內容.远程调用过程中,其参数都被打包成Parcel的形式來传递.IBinder对象当然也不例外,在前 ...

  7. Binder机制,从Java到C (6. Binder in Native : libbinder)

    1.Java和C++中的Binder 从前一篇 Binder机制,从Java到C (5. IBinder对象传递形式) 中可以看到,使用Binder的Java代码,到最后都会进入到Native环境,将 ...

  8. Android中的Binder机制的简要理解

    转载自:http://www.linuxidc.com/Linux/2012-07/66195.htm http://blog.csdn.net/sunxingzhesunjinbiao/articl ...

  9. Android深入浅出之Binder机制

    一说明 Android系统最常见也是初学者最难搞明白的就是Binder了,很多很多的Service就是通过Binder机制来和客户端通讯交互的.所以搞明白Binder的话,在很大程度上就能理解程序运行 ...

随机推荐

  1. c#之Async、Await剖析

    c#之Async.Await剖析 探索c#之Async.Await剖析 2015-06-15 08:35 by 蘑菇先生, 1429 阅读, 5 评论, 收藏, 编辑 阅读目录: 基本介绍 基本原理剖 ...

  2. php+flash头像上传组件

    有会员系统的站点一般都会有一个头像上传组件,一般做的最简单的是 这样的方式长处是代码写的简单,仅仅要推断图片大小和类型,然后更新数据库.可是用户体验不高.并且站点其它页面假设要使用较小的20X20或1 ...

  3. oracle_powerdesinger逆向工程 , PDM 文件 注释到name的完美解决方案 comment2name

    1. 从oracle 到 PDM文件  逆向工程中 ,需要注意 去掉“” ,这个百度下很多帖子,用于去掉引号 2. 从注释copy到name运行脚本会有个问题就是 ,有些注释太长,不美观 解决方案, ...

  4. ASP.NET MVC学习之控制器篇扩展性

    原文:ASP.NET MVC学习之控制器篇扩展性 一.前言 在之前的一篇随笔中已经讲述过控制器,而今天的随笔是作为之前的扩展. 二.正文 1.自定义动作方法 相信大家在开发过程一定会遇到动作方法的重名 ...

  5. 房间计费系统改造E-R图纸设计

    简单的学习过程:     这几天忙得太混乱了,用了近一个星期才设计好.我在这段时间遇到的困难,就积极找师哥师姐指点迷津,如今多少总算是有些拿得出手的成果. 学习成果: Entity Relations ...

  6. 蓝桥杯 BASIC 27 矩阵乘法(矩阵、二维数组)

    [思路]:注意0次幂是单位矩阵. [AC代码]: #include <iostream> #include <algorithm> #include <iomanip&g ...

  7. Xcode6为什么干掉pch(Precompile Prefix Header)&amp;怎样加入pch文件

    一直在用xcode6开发,但项目都是在xcode5上创建的,所以一直没注意到,xcode6居然干掉pch文件了. 为什么xcode6没有自己主动创建pch文件呢? 简单地看:我们在写项目的时候,大部分 ...

  8. SharePoint 2013 搜索SharePoint 特定列和特定文档(自己定义搜索)

    SharePoint 2013 搜索SharePoint 特定列和特定文档 1,操作步骤和图例,因语言和版本号的不同 我尽量使用抓图方式. 2.  In Central Administration, ...

  9. CentOS 使用yum命令安装Java SDK(openjdk)

    CentOS 6.X 和 5.X 自带有OpenJDK runtime environment  (openjdk).它是一个在linux上实现开源的java 平台.CentOS  yum 命令 安装 ...

  10. phpstorm安装laravel-ide-helper实现自动完成、代码提示和跟踪

    本文讲述laravel-ide-helper的安装方法.phpstorm安装了laravel-ide-helper后可以实现代码提示.跟踪和自动补全,减少查看API文档的次数,提高开发效率. lara ...