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

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

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

这里首先把Java语言实现的Parcel对象class转换成C++语言实现的Parcel对象parcel,接着,通过parcel->readStrongBinder函数来获得一个Binder引用。

我们在前面学习Binder机制时,在Android系统进程间通信(IPC)机制Binder中的Client获得Server远程接口过程源代码分析这篇文章中,曾经分析过这个函数,它最终返回来的是一个BpBinder对象,因此,下面的语句:

  1. return javaObjectForIBinder(env, parcel->readStrongBinder());  

       就相当于是:

  1. return javaObjectForIBinder(env, new BpBinder(handle));  

        这里的handle就是HelloService这个Binder实体在Client进程中的句柄了,它是由Binder驱动程序设置的,上层不用关心它的值具体是多少。至于javaObjectForIBinder这个函数,我们前面介绍如何获取Service Manager的Java远程接口时已经有详细介绍,这里就不累述了,它的作用就是创建一个BinderProxy对象,并且把刚才获得的BpBinder对象的地址保存在这个BinderProxy对象的mObject成员变量中。

       最后返回到Hello.onCreate函数中,从下面语句返回:

  1. helloService = IHelloService.Stub.asInterface(    
  2.                     ServiceManager.getService("hello"));  

       就相当于是:

  1. helloService = IHelloService.Stub.asInterface(new BinderProxy()));   

      回忆一下前面介绍IHelloService接口的定义时,IHelloService.Stub.asInterface是这样定义的:

  1. public interface IHelloService extends Android.os.IInterface  
  2. {  
  3.     /** Local-side IPC implementation stub class. */  
  4.     public static abstract class Stub extends android.os.Binder implements android.os.IHelloService  
  5.     {  
  6.         ......  
  7.   
  8.         public static android.os.IHelloService asInterface(android.os.IBinder obj)  
  9.         {  
  10.             if ((obj==null)) {  
  11.                 return null;  
  12.             }  
  13.             android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);  
  14.             if (((iin!=null)&&(iin instanceof android.os.IHelloService))) {  
  15.                 return ((android.os.IHelloService)iin);  
  16.             }  
  17.             return new android.os.IHelloService.Stub.Proxy(obj);  
  18.         }  
  19.   
  20.         ......  
  21.     }  
  22. }  

这里的obj是一个BinderProxy对象,它的queryLocalInterface返回null,于是调用下面语句获得HelloService的远程接口:

  1. return new android.os.IHelloService.Stub.Proxy(obj);  

相当于是:

  1. return new android.os.IHelloService.Stub.Proxy(new BinderProxy());  

        这样,我们就获得了HelloService的远程接口了,它实质上是一个实现了IHelloService接口的IHelloService.Stub.Proxy对象。

        五. Client通过HelloService的Java远程接口来使用HelloService提供的服务的过程

        上面介绍的Hello这个Activity获得了HelloService的远程接口后,就可以使用它的服务了。

        我们以使用IHelloService.getVal函数为例详细说明。在Hello::onClick函数中调用了IHelloService.getVal函数:

  1. public class Hello extends Activity implements OnClickListener {  
  2.     ......  
  3.   
  4.     @Override  
  5.     public void onClick(View v) {  
  6.         if(v.equals(readButton)) {  
  7.             int val = helloService.getVal();    
  8.             ......  
  9.         }  
  10.         else if(v.equals(writeButton)) {  
  11.             ......  
  12.         }  
  13.         else if(v.equals(clearButton)) {  
  14.             ......  
  15.         }  
  16.     }  
  17.   
  18.     ......  
  19. }  

         通知前面的分析,我们知道,这里的helloService接口实际上是一个IHelloService.Stub.Proxy对象,因此,我们进入到IHelloService.Stub.Proxy类的getVal函数中:

  1. public interface IHelloService extends android.os.IInterface  
  2. {  
  3.     /** Local-side IPC implementation stub class. */  
  4.     public static abstract class Stub extends android.os.Binder implements android.os.IHelloService  
  5.     {  
  6.           
  7.         ......  
  8.   
  9.         private static class Proxy implements android.os.IHelloService  
  10.         {  
  11.             private android.os.IBinder mRemote;  
  12.   
  13.             ......  
  14.   
  15.             public int getVal() throws android.os.RemoteException  
  16.             {  
  17.                 android.os.Parcel _data = android.os.Parcel.obtain();  
  18.                 android.os.Parcel _reply = android.os.Parcel.obtain();  
  19.                 int _result;  
  20.                 try {  
  21.                     _data.writeInterfaceToken(DESCRIPTOR);  
  22.                     mRemote.transact(Stub.TRANSACTION_getVal, _data, _reply, 0);  
  23.                     _reply.readException();  
  24.                     _result = _reply.readInt();  
  25.                 }  
  26.                 finally {  
  27.                     _reply.recycle();  
  28.                     _data.recycle();  
  29.                 }  
  30.                 return _result;  
  31.             }  
  32.         }  
  33.   
  34.         ......  
  35.         static final int TRANSACTION_getVal = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);  
  36.     }  
  37.   
  38.     ......  
  39. }  

         这里我们可以看出,实际上是通过mRemote.transact来请求HelloService执行TRANSACTION_getVal操作。这里的mRemote是一个BinderProxy对象,这是我们在前面获取HelloService的Java远程接口的过程中创建的。

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

       

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