上一篇 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. Node.js新手教程——怎样实现文件上传功能

    作者:zhanhailiang 日期:2014-11-16 本文将介绍怎样使用Node.js实现文件上传功能. 1. 初始化项目信息:npm init [root@~/wade/nodejs/node ...

  2. XP下类似%windir% %userprofile% 的变量的说明(转)

    在一些批处理或者系统技巧操作教程文章中,我们常常会看到一些形如 %windir% 或者 %systemdrive% 的变量.这些变量都代表着什么含义呢?下面小技巧之家为大家整理了在Windows XP ...

  3. 数学思想方法-sasMEMO(17)

    SAS日期及时间格式 data  _null_;input mydate YYMMDD10.;put mydate YYMMDDB10.;put mydate YYMMDDC10.;put mydat ...

  4. 求和问题(DFS)

    输入: n=4 a={1,2,4,7} k=13 输出: YES 输入: n=4 a={1,2,4,7} k=13 输出: NO 选出若干数,使它们的和恰好为k #include <stdio. ...

  5. 至linuxNIC添加多个IP

    由于工作的需要,只是有一个2以太网端口server构造3个月IP.制linux. 整理如以下的现在的过程,有离开后,,学生们也将不能够引用. IP1:10.110.97.68 IP2:10.115.2 ...

  6. AlloyRenderingEngine

    AlloyRenderingEngine燃烧的进度条 写在前面 Github: https://github.com/AlloyTeam/AlloyGameEngine HTML 5新增了progre ...

  7. ASP.NET上传文件的几种方法

      //上传文件实例 if (fileDealer.HasFile)//判断文件是否存在        {            string filepath = "";     ...

  8. Cloudera CDH 5集群搭建(yum 方式)

    1      集群环境 主节点 master001 ~~ master006 从节点 slave001 ~~ slave064 2      安装CDH5的YUM源 rpm -Uvhhttp://ar ...

  9. js中arguments

    arguments 每天一对象,JS天天见,今天我们来看看arguments对象及属性.arguments对象不能显式创建,arguments对象只有函数开始时才可用.函数的 arguments 对象 ...

  10. crawler_网络爬虫之数据分析_httpwatcher

    所谓爬虫,首先要通过各种手段爬取到想要站点的数据. web2.0之后,各种网络站点类型越来越多,早期的站点多为静态页面[html .htm],后来逐步加入 jsp.asp,等交互性强的页面.再后来随着 ...