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

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

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

二. HelloService接口的定义

前面我们在学习Android系统的硬件抽象层(HAL)时,在在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务这篇文章中,我们编写了一个硬件服务HelloService,它的服务接口定义在frameworks/base/core/java/android/os/IHelloService.aidl文件中:

  1. package android.os;  
  2.   
  3. interface IHelloService  
  4. {  
  5.     void setVal(int val);  
  6.     int getVal();  
  7. }  

 

       这个服务接口很简单,只有两个函数,分别用来读写硬件寄存器。

       注意,这是一个aidl文件,编译后会生成一个IHelloService.java。我们来看一下这个文件的内容隐藏着什么奥秘,可以这么神奇地支持进程间通信。

  1. /* 
  2.  * This file is auto-generated.  DO NOT MODIFY. 
  3.  * Original file: frameworks/base/core/java/android/os/IHelloService.aidl 
  4.  */  
  5. package android.os;  
  6. public interface IHelloService extends android.os.IInterface  
  7. {  
  8.     /** Local-side IPC implementation stub class. */  
  9.     public static abstract class Stub extends android.os.Binder implements android.os.IHelloService  
  10.     {  
  11.         private static final java.lang.String DESCRIPTOR = "android.os.IHelloService";  
  12.         /** Construct the stub at attach it to the interface. */  
  13.         public Stub()  
  14.         {  
  15.             this.attachInterface(this, DESCRIPTOR);  
  16.         }  
  17.   
  18.         /** 
  19.         * Cast an IBinder object into an android.os.IHelloService interface, 
  20.         * generating a proxy if needed. 
  21.         */  
  22.         public static android.os.IHelloService asInterface(android.os.IBinder obj)  
  23.         {  
  24.             if ((obj==null)) {  
  25.                 return null;  
  26.             }  
  27.             android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);  
  28.             if (((iin!=null)&&(iin instanceof android.os.IHelloService))) {  
  29.                 return ((android.os.IHelloService)iin);  
  30.             }  
  31.             return new android.os.IHelloService.Stub.Proxy(obj);  
  32.         }  
  33.   
  34.         public android.os.IBinder asBinder()  
  35.         {  
  36.             return this;  
  37.         }  
  38.   
  39.         @Override   
  40.         public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException  
  41.         {  
  42.             switch (code)  
  43.             {  
  44.                 case INTERFACE_TRANSACTION:  
  45.                 {  
  46.                     reply.writeString(DESCRIPTOR);  
  47.                     return true;  
  48.                 }  
  49.                 case TRANSACTION_setVal:  
  50.                 {  
  51.                     data.enforceInterface(DESCRIPTOR);  
  52.                     int _arg0;  
  53.                     _arg0 = data.readInt();  
  54.                     this.setVal(_arg0);  
  55.                     reply.writeNoException();  
  56.                     return true;  
  57.                 }  
  58.                 case TRANSACTION_getVal:  
  59.                 {  
  60.                     data.enforceInterface(DESCRIPTOR);  
  61.                     int _result = this.getVal();  
  62.                     reply.writeNoException();  
  63.                     reply.writeInt(_result);  
  64.                     return true;  
  65.                 }  
  66.             }  
  67.             return super.onTransact(code, data, reply, flags);  
  68.         }  
  69.   
  70.         private static class Proxy implements android.os.IHelloService  
  71.         {  
  72.             private android.os.IBinder mRemote;  
  73.   
  74.             Proxy(android.os.IBinder remote)  
  75.             {  
  76.                 mRemote = remote;  
  77.             }  
  78.   
  79.             public android.os.IBinder asBinder()  
  80.             {  
  81.                 return mRemote;  
  82.             }  
  83.   
  84.             public java.lang.String getInterfaceDescriptor()  
  85.             {  
  86.                 return DESCRIPTOR;  
  87.             }  
  88.   
  89.             public void setVal(int val) throws android.os.RemoteException  
  90.             {  
  91.                 android.os.Parcel _data = android.os.Parcel.obtain();  
  92.                 android.os.Parcel _reply = android.os.Parcel.obtain();  
  93.                 try {  
  94.                     _data.writeInterfaceToken(DESCRIPTOR);  
  95.                     _data.writeInt(val);  
  96.                     mRemote.transact(Stub.TRANSACTION_setVal, _data, _reply, 0);  
  97.                     _reply.readException();  
  98.                 }  
  99.                 finally {  
  100.                     _reply.recycle();  
  101.                     _data.recycle();  
  102.                 }  
  103.             }  
  104.   
  105.             public int getVal() throws android.os.RemoteException  
  106.             {  
  107.                 android.os.Parcel _data = android.os.Parcel.obtain();  
  108.                 android.os.Parcel _reply = android.os.Parcel.obtain();  
  109.                 int _result;  
  110.                 try {  
  111.                     _data.writeInterfaceToken(DESCRIPTOR);  
  112.                     mRemote.transact(Stub.TRANSACTION_getVal, _data, _reply, 0);  
  113.                     _reply.readException();  
  114.                     _result = _reply.readInt();  
  115.                 }  
  116.                 finally {  
  117.                     _reply.recycle();  
  118.                     _data.recycle();  
  119.                 }  
  120.                 return _result;  
  121.             }  
  122.         }  
  123.   
  124.         static final int TRANSACTION_setVal = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);  
  125.         static final int TRANSACTION_getVal = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);  
  126.     }  
  127.   
  128.     public void setVal(int val) throws android.os.RemoteException;  
  129.     public int getVal() throws android.os.RemoteException;  
  130. }  

         这里我们可以看到IHelloService.aidl这个文件编译后的真面目,原来就是根据IHelloService接口的定义生成相应的Stub和Proxy类,这个就是我们熟悉的Binder机制的内容了,即实现这个HelloService的Server必须继续于这里的IHelloService.Stub类,而这个HelloService的远程接口就是这里的IHelloService.Stub.Proxy对象获得的IHelloService接口。接下来的内容,我们就可以看到IHelloService.Stub和IHelloService.Stub.Proxy是怎么创建或者使用的。

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

       

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