你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2011-07-31] 来源:csdn  作者:Luoshengyang [字体:大 中 小]

具体可以参考Android系统进程间通信(IPC)机制Binder中的Server启动过程源代码分析一文。需要注意的是,这里的data包含了一个JavaBBinderHolder类型的Binder实体对象,它就代表了我们上面创建的HelloService。Service Manager收到这个ADD_SERVICE_TRANSACTION请求时,就会把这个Binder实体纳入到自己内部进行管理。

这样,实现HelloService的Server的启动过程就完成了。

四. Client获取HelloService的Java远程接口的过程

前面我们在学习Android系统硬件抽象层(HAL)时,在在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务这篇文章中,我们创建了一个应用程序,这个应用程序作为一个Client角色,借助Service Manager这个Java远程接口来获得HelloService的远程接口,进而调用HelloService提供的服务。

我们看看它是如何借助Service Manager这个Java远程接口来获得HelloService的远程接口的。在Hello这个Activity的onCreate函数,通过IServiceManager.getService函数来获得HelloService的远程接口:

  1. public class Hello extends Activity implements OnClickListener {    
  2.     ......   
  3.   
  4.     private IHelloService helloService = null;    
  5.   
  6.     ......  
  7.   
  8.     @Override    
  9.     public void onCreate(Bundle savedInstanceState) {    
  10.   
  11.         helloService = IHelloService.Stub.asInterface(    
  12.                             ServiceManager.getService("hello"));  
  13.     }  
  14.   
  15.     ......  
  16. }  

         我们先来看ServiceManager.getService的实现。前面我们说过,这里实际上是调用了ServiceManagerProxy.getService函数:

  1. class ServiceManagerProxy implements IServiceManager {  
  2.     public ServiceManagerProxy(IBinder remote) {  
  3.         mRemote = remote;  
  4.     }  
  5.   
  6.     ......  
  7.   
  8.     public IBinder getService(String name) throws RemoteException {  
  9.         Parcel data = Parcel.obtain();  
  10.         Parcel reply = Parcel.obtain();  
  11.         data.writeInterfaceToken(IServiceManager.descriptor);  
  12.         data.writeString(name);  
  13.         mRemote.transact(GET_SERVICE_TRANSACTION, data, reply, 0);  
  14.         IBinder binder = reply.readStrongBinder();  
  15.         reply.recycle();  
  16.         data.recycle();  
  17.         return binder;  
  18.     }  
  19.   
  20.     ......  
  21.   
  22.     private IBinder mRemote;  
  23. }  

最终通过mRemote.transact来执行实际操作。我们在前面已经介绍过了,这里的mRemote实际上是一个BinderProxy对象,它的transact成员函数是一个JNI方法,实现在frameworks/base/core/jni/android_util_Binder.cpp文件中的android_os_BinderProxy_transact函数中。

这个函数前面我们已经看到了,这里就不再列出来了。不过,当这个函数从:

  1. status_t err = target->transact(code, *data, reply, flags);  

       这里的reply变量里面就包括了一个HelloService的引用了。注意,这里的reply变量就是我们在ServiceManagerProxy.getService函数里面传进来的参数reply,它是一个Parcel对象。

回到ServiceManagerProxy.getService函数中,从下面语句返回:

  1. mRemote.transact(GET_SERVICE_TRANSACTION, data, reply, 0);  

接着,就通过下面语句将这个HelloService的引用读出来:

  1. IBinder binder = reply.readStrongBinder();  

我们看看Parcel.readStrongBinder的实现:

  1. public final class Parcel {  
  2.     ......  
  3.   
  4.     /** 
  5.     * Read an object from the parcel at the current dataPosition(). 
  6.     */  
  7.     public final native IBinder readStrongBinder();  
  8.   
  9.     ......  
  10. }  

它也是一个JNI方法,实现在frameworks/base/core/jni/android_util_Binder.cpp文件中:

  1. static jobject android_os_Parcel_readStrongBinder(JNIEnv* env, jobject clazz)  
  2. {  
  3.     Parcel* parcel = parcelForJavaObject(env, clazz);  
  4.     if (parcel != NULL) {  
  5.         return javaObjectForIBinder(env, parcel->readStrongBinder());  
  6.     }  
  7.     return NULL;  
  8. }  
linux
相关资讯       Android教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款