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

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

[日期:2011-07-31] 来源:csdn  作者:Luoshengyang [字体:大 中 小]
它实际上只做了一件事情,就是创建一个JavaBBinderHolder对象jbh,然后把这个对象的地址保存在上面的Binder类的mObject成员变量中,后面我们会用到。

回到ServerThread.run函数中,我们再来看一下ServiceManager.addService函数的实现:

  1. public final class ServiceManager {  
  2.     ......  
  3.   
  4.     private static IServiceManager sServiceManager;  
  5.   
  6.     ......  
  7.   
  8.     public static void addService(String name, IBinder service) {  
  9.         try {  
  10.             getIServiceManager().addService(name, service);  
  11.         } catch (RemoteException e) {  
  12.             Log.e(TAG, "error in addService", e);  
  13.         }  
  14.     }  
  15.   
  16.     ......  
  17.   
  18. }  

         这里的getIServiceManager函数我们在前面已经分析过了,它返回的是一个ServiceManagerProxy对象的IServiceManager接口。因此,我们进入到ServiceManagerProxy.addService中去看看:

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

       这里的Parcel类是用Java来实现的,它跟我们前面几篇文章介绍Binder机制时提到的用C++实现的Parcel类的作用是一样的,即用来在两个进程之间传递数据。

       这里我们关注是如何把参数service写到data这个Parcel对象中去的:

  1. data.writeStrongBinder(service);  

       我们来看看Parcel.writeStrongBinder函数的实现:

  1. public final class Parcel {  
  2.     ......  
  3.   
  4.     /** 
  5.     * Write an object into the parcel at the current dataPosition(), 
  6.     * growing dataCapacity() if needed. 
  7.     */  
  8.     public final native void writeStrongBinder(IBinder val);  
  9.   
  10.     ......  
  11. }  

        这里的writeStrongBinder函数又是一个JNI方法,它定义在frameworks/base/core/jni/Android_util_Binder.cpp文件中:

  1. static void android_os_Parcel_writeStrongBinder(JNIEnv* env, jobject clazz, jobject object)  
  2. {  
  3.     Parcel* parcel = parcelForJavaObject(env, clazz);  
  4.     if (parcel != NULL) {  
  5.         const status_t err = parcel->writeStrongBinder(ibinderForJavaObject(env, object));  
  6.         if (err != NO_ERROR) {  
  7.             jniThrowException(env, "java/lang/OutOfMemoryError", NULL);  
  8.         }  
  9.     }  
  10. }  
       这里的clazz参数是一个Java语言实现的Parcel对象,通过parcelForJavaObject把它转换成C++语言实现的Parcel对象。这个函数的实现我们就不看了,有兴趣的读者可以研究一下,这个函数也是实现在frameworks/base/core/jni/android_util_Binder.cpp这个文件中���
       这里的object参数是一个Java语言实现的Binder对象,在调用C++语言实现的Parcel::writeStrongBinder把这个对象写入到parcel对象时,首先通过ibinderForJavaObject函数把这个Java语言实现的Binder对象转换为C++语言实现的JavaBBinderHolder对象:
  1. sp<IBinder> ibinderForJavaObject(JNIEnv* env, jobject obj)  
  2. {  
  3.     if (obj == NULL) return NULL;  
  4.   
  5.     if (env->IsInstanceOf(obj, gBinderOffsets.mClass)) {  
  6.         JavaBBinderHolder* jbh = (JavaBBinderHolder*)  
  7.             env->GetIntField(obj, gBinderOffsets.mObject);  
  8.         return jbh != NULL ? jbh->get(env) : NULL;  
  9.     }  
  10.   
  11.     if (env->IsInstanceOf(obj, gBinderProxyOffsets.mClass)) {  
  12.         return (IBinder*)  
  13.             env->GetIntField(obj, gBinderProxyOffsets.mObject);  
  14.     }  
  15.   
  16.     LOGW("ibinderForJavaObject: %p is not a Binder object", obj);  
  17.     return NULL;  
  18. }  

         我们知道,这里的obj参数是一个Binder类的实例,因此,这里会进入到第一个if语句中去。

         在前面创建HelloService对象,曾经在调用到HelloService的父类Binder中,曾经在JNI层创建了一个JavaBBinderHolder对象,然后把这个对象的地址保存在Binder类的mObject成员变量中,因此,这里把obj对象的mObject成员变量强制转为JavaBBinderHolder对象。

         到了这里,这个函数的功课还未完成,还剩下最后关键的一步:

  1. return jbh != NULL ? jbh->get(env) : NULL;  

        这里就是jbh->get这个语句了。

linux
相关资讯       Android教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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