上一篇 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. Oracle 用户权限管理方法

    Oracle 用户权限管理方法 sys;//系统管理员,拥有最高权限 system;//本地管理员,次高权限 scott;//普通用户,密码默认为tiger,默认未解锁 sys;//系统管理员,拥有最 ...

  2. 读取xml文件"分析 EntityName 时出错"的解决方案

    在涉及到xml与xslt编程的过程中,经常会碰到"分析 EntityName 时出错"的提示,这个不是程序错误,是因为xml文件中使用了一些特殊符号导致的.    XML 节点中不 ...

  3. javascript系列之变量对象

    原文:javascript系列之变量对象 引言 一般在编程的时候,我们会定义函数和变量来成功的构造我们的系统.但是解析器该如何找到这些数据(函数,变量)呢?当我们引用需要的对象时,又发生了什么了? 很 ...

  4. SQL Profile (总结4)--使用演示示例

    前提:sql profile工具的相关视图 dba_sql_profile 10g: sys.sqlprof$attr  &  sqlprof$ 11g: sys.sqlobj$data  & ...

  5. Net下无敌的ORM

    Dapper ORM 用法—Net下无敌的ORM http://www.renfb.com/blog/2011/Article/335 假如你喜欢原生的Sql语句,又喜欢ORM的简单,那你一定会喜欢上 ...

  6. mysql_自动备份

    1 @echo off 2 set errorlevel=0 3 set dump_command="C:\Program Files\MySQL\MySQL Server 5.5\bin\ ...

  7. MD5处理图片加密算法

    Android MD5加密算与J2SE平台一模一样,由于Android 平台支持 java.security.MessageDigest这个包.实际上与J2SE平台一模一样. 首先: 输入一个Stri ...

  8. ResultSet 转为 List或者JavaBean

    一.将ResultSet结果集转换为List,其中每条记录信息保存为Map放到List中,方法如下: public static List<Map<String, Object>&g ...

  9. c++中string类的具体解释

    通过在站点上的资料搜集,得到了非常多关于string类使用方法的文档,通过对这些资料的整理和增加一些自己的代码,就得出了一份比較完整的关于string类函数有哪些和如何用的文档了! 以下先罗列出str ...

  10. ASP.NET MVC中加载WebForms用户控件(.ascx)

    原文:ASP.NET MVC中加载WebForms用户控件(.ascx) 问题背景 博客园博客中的日历用的是ASP.NET WebForms的日历控件(System.Web.UI.WebControl ...