现在的位置: 首页 > 综合 > 正文

Android Java basic knowledge —AIDL1

2013年12月04日 ⁄ 综合 ⁄ 共 14995字 ⁄ 字号 评论关闭

AIDL (Android Interface Definition Language) 是一种接口定义语言,用于生成代码允许Android设备上的两个进程间进程通信(IPC). 
如果你需要编写一个进程(比如Activity)访问另一个进程(比如Services)的对象的方法代码,你可以使用AIDL自动生成代码而不用自己配置大量的参数. 
AIDL IPC基于接口机制,类似COM,Corba,且更加轻量化.它使用一个代理来在客户和实现间传递值. 

Implementing IPC Using AIDL 实现进程通信IPC 

分为五个步骤: 
1.创建 SomeService.aidl 文件 
利用aidl.exe生成接口文件.若你的IDE安装了ADT,将会在gen目录或src相应包中自动根据描述文件生成同名接口文件.否则请手动: 

命令行: 
adil pathSomeService.adil <CR> 

注意: 
1.自定义类在aidl描述文件中,即便在同一个包中,也要显式import. 
2.在aidl文件中所有非Java原始类型参数必须加上标记:in, out, inout. 
3.Java 原始类型默认为in,且不能为其它. 
4.Java 原始类型包括为java.lang, java,util包中包括的类. 
5.接口名同aidl文件名. 
6.接口前不用加访问权限修饰符public ,private, protected等,也不能用final ,static.
 

接口文件分析: 

接口中生成一个Stub的抽象类,里面包括aidl定义的方法.还包括一些其它辅助方法.值得关注的是asInterface(IBinder iBinder),它返回接口的实例. 

2.实现接口 
接口的实现需继承接口.Stub.并实现Stub类的方法. 
下面给出一个使用匿名方式实现的例子. 

private final SomeService.Stub binder = new SomeService.Stub(){ 
      public void service(){ 
         //... 
      } 

注意: 
1.没有异常会正常返回 
2.RPC通常比较耗时且是异步的,因此应该在线程中调用RPC服务. 
3.只支持方法,不支持静态字段. 

3.暴露接口给客户 
客户要服务,当然要知道在哪有服务.通常一台服务器可能提供不止一个服务.因些,我们使用 
RomoteService来管理所有远程服务. 
暴露服务必须继承Service.并实现onBind()方法.

Java代码
  1. public   class  RemoteService  extends  Service {   
  2. ...   
  3.      @Override   
  4.      public  IBinder onBind(Intent intent) {   
  5.          // Select the interface to return.  If your service only implements   
  6.          // a single interface, you can just return it here without checking   
  7.          // the Intent.   
  8.          if  (SomeService. class .getName().equals(intent.getAction())) {   
  9.              return  mBinder;   
  10.         }   
  11.          if  (ISecondary. class .getName().equals(intent.getAction())) {   
  12.              return  mSecondaryBinder;   
  13.         }   
  14.          return   null ;   
  15.     }   
  16.   
  17.      /**  
  18.      * The SomeService Interface is defined through IDL  
  19.      */   
  20.      private   final  SomeService.Stub mBinder =  new  SomeService.Stub() {   
  21.          public   void  registerCallback(SomeServiceCallback cb) {   
  22.              if  (cb !=  null ) mCallbacks.register(cb);   
  23.         }   
  24.          public   void  unregisterCallback(SomeServiceCallback cb) {   
  25.              if  (cb !=  null ) mCallbacks.unregister(cb);   
  26.         }   
  27.     };   
  28.   
  29.      /**  
  30.      * A secondary interface to the service.  
  31.      */   
  32.      private   final  ISecondary.Stub mSecondaryBinder =  new  ISecondary.Stub() {   
  33.          public   int  getPid() {   
  34.              return  Process.myPid();   
  35.         }   
  36.          public   void  basicTypes( int  anInt,  long  aLong,  boolean  aBoolean,   
  37.                  float  aFloat,  double  aDouble, String aString) {   
  38.         }   
  39.     };   
  40.   
  41. }  
public class RemoteService extends Service {
...
    @Override
    public IBinder onBind(Intent intent) {
        // Select the interface to return.  If your service only implements
        // a single interface, you can just return it here without checking
        // the Intent.
        if (SomeService.class.getName().equals(intent.getAction())) {
            return mBinder;
        }
        if (ISecondary.class.getName().equals(intent.getAction())) {
            return mSecondaryBinder;
        }
        return null;
    }

    /**
     * The SomeService Interface is defined through IDL
     */
    private final SomeService.Stub mBinder = new SomeService.Stub() {
        public void registerCallback(SomeServiceCallback cb) {
            if (cb != null) mCallbacks.register(cb);
        }
        public void unregisterCallback(SomeServiceCallback cb) {
            if (cb != null) mCallbacks.unregister(cb);
        }
    };

    /**
     * A secondary interface to the service.
     */
    private final ISecondary.Stub mSecondaryBinder = new ISecondary.Stub() {
        public int getPid() {
            return Process.myPid();
        }
        public void basicTypes(int anInt, long aLong, boolean aBoolean,
                float aFloat, double aDouble, String aString) {
        }
    };

}

这样暴露好了服务,接着做什么?当然是调用服务了. 

调用之前,有必要了解下 对象打包,类似与Java的对象序列化. 

4.使用打包传送参数 

如果一个类要使用打包功能(类似对象序列化),要实现如下5个步骤: 

4.1 实现 Parcelable接口 
4.2 实现 public void writeToParcel(Parcel out) 方法 
4.3 实现 public void readFromParcel(Parcel in) 方法 
4.4 添加一个静态字段 CREATOR 到实现 Parcelable.Creator 接口的类中 
4.5 创建一个aidl文件声明你的可打包的类 

示例: 
Rect.java

Java代码
  1. import  android.os.Parcel;   
  2. import  android.os.Parcelable;   
  3.   
  4. public   final   class  Rect  implements  Parcelable {   
  5.      public   int  left;   
  6.      public   int  top;   
  7.      public   int  right;   
  8.      public   int  bottom;   
  9.   
  10.      public   static   final  Parcelable.Creator<Rect> CREATOR =  new  Parcelable.Creator<Rect>() {   
  11.          public  Rect createFromParcel(Parcel in) {   
  12.              return   new  Rect(in);   
  13.         }   
  14.   
  15.          public  Rect[] newArray( int  size) {   
  16.              return   new  Rect[size];   
  17.         }   
  18.     };   
  19.   
  20.      public  Rect() {   
  21.     }   
  22.   
  23.      private  Rect(Parcel in) {   
  24.         readFromParcel(in);   
  25.     }   
  26.   
  27.      public   void  writeToParcel(Parcel out) {   
  28.         out.writeInt(left);   
  29.         out.writeInt(top);   
  30.         out.writeInt(right);   
  31.         out.writeInt(bottom);   
  32.     }   
  33.   
  34.      public   void  readFromParcel(Parcel in) {   
  35.         left = in.readInt();   
  36.         top = in.readInt();   
  37.         right = in.readInt();   
  38.         bottom = in.readInt();   
  39.     }   
  40. }  
import android.os.Parcel;
import android.os.Parcelable;

public final class Rect implements Parcelable {
    public int left;
    public int top;
    public int right;
    public int bottom;

    public static final Parcelable.Creator<Rect> CREATOR = new Parcelable.Creator<Rect>() {
        public Rect createFromParcel(Parcel in) {
            return new Rect(in);
        }

        public Rect[] newArray(int size) {
            return new Rect[size];
        }
    };

    public Rect() {
    }

    private Rect(Parcel in) {
        readFromParcel(in);
    }

    public void writeToParcel(Parcel out) {
        out.writeInt(left);
        out.writeInt(top);
        out.writeInt(right);
        out.writeInt(bottom);
    }

    public void readFromParcel(Parcel in) {
        left = in.readInt();
        top = in.readInt();
        right = in.readInt();
        bottom = in.readInt();
    }
}

Rect.aidl

Java代码
  1. package  android.graphics;   
  2.   
  3. // Declare Rect so AIDL can find it and knows that it implements   
  4. // the parcelable protocol.   
  5. parcelable Rect;  
package android.graphics;

// Declare Rect so AIDL can find it and knows that it implements
// the parcelable protocol.
parcelable Rect;

注意: 
参数一定不能越界. 

5.调用IPC方法 
调用IPC方法还有6个步骤: 
如果觉得烦,那就尽力弄懂,不然你不晕,我译得也晕.. 

5.1 声明aidl定义的接口类型引用 
5.2 实现 ServiceConnection 
5.3 调用 Context.bindService(),传入 ServiceConnection 的实现 
5.4 在你的 ServiceConnection.onServiceConnected(),你将得到一个 IBinder 实例(service). 调用  YourInterfaceName.Stub.asInterface((IBinder)service)强制转换 YourInterface 类型. 
5.5 调用接口定义的方法.你应该始终小心 DeadObjectException 异常,当连接不成功或中断它就会抛出,这也是远程对象唯一的一个异常. 
5.6 断开连接,调用 Context.unbindService(). 

注解: 
你可以使用匿名对象作为参数. 
对象是引用计数. 

ApiDemos 有个子the Remote Activity 的例子. 

Java代码
  1. public   class  RemoteServiceBinding  extends  Activity {   
  2.      /** The primary interface we will be calling on the service. */   
  3.     IRemoteService mService =  null ;   
  4.      /** Another interface we use on the service. */   
  5.     ISecondary mSecondaryService =  null ;   
  6.   
  7.     Button mKillButton;   
  8.     TextView mCallbackText;   
  9.   
  10.      private   boolean  mIsBound;   
  11.   
  12.      /**  
  13.      * Standard initialization of this activity.  Set up the UI, then wait  
  14.      * for the user to poke it before doing anything.  
  15.      */   
  16.      @Override   
  17.      protected   void  onCreate(Bundle savedInstanceState) {   
  18.          super .onCreate(savedInstanceState);   
  19.   
  20.         setContentView(R.layout.remote_service_binding);   
  21.   
  22.          // Watch for button clicks.   
  23.         Button button = (Button)findViewById(R.id.bind);   
  24.         button.setOnClickListener(mBindListener);   
  25.         button = (Button)findViewById(R.id.unbind);   
  26.         button.setOnClickListener(mUnbindListener);   
  27.         mKillButton = (Button)findViewById(R.id.kill);   
  28.         mKillButton.setOnClickListener(mKillListener);   
  29.         mKillButton.setEnabled( false );   
  30.   
  31.         mCallbackText = (TextView)findViewById(R.id.callback);   
  32.         mCallbackText.setText( "Not attached." );   
  33.     }   
  34.   
  35.      /**  
  36.      * Class for interacting with the main interface of the service.  
  37.      */   
  38.      private  ServiceConnection mConnection =  new  ServiceConnection() {   
  39.          public   void  onServiceConnected(ComponentName className,   
  40.                 IBinder service) {   
  41.              // This is called when the connection with the service has been   
  42.              // established, giving us the service object we can use to   
  43.              // interact with the service.  We are communicating with our   
  44.              // service through an IDL interface, so get a client-side   
  45.              // representation of that from the raw service object.   
  46.             mService = IRemoteService.Stub.asInterface(service);   
  47.             mKillButton.setEnabled( true );   
  48.             mCallbackText.setText( "Attached." );   
  49.   
  50.              // We want to monitor the service for as long as we are   
  51.              // connected to it.   
  52.              try  {   
  53.                 mService.registerCallback(mCallback);   
  54.             }  catch  (RemoteException e) {   
  55.                  // In this case the service has crashed before we could even   
  56.                  // do anything with it; we can count on soon being   
  57.                  // disconnected (and then reconnected if it can be restarted)   
  58.                  // so there is no need to do anything here.   
  59.             }   
  60.   
  61.              // As part of the sample, tell the user what happened.   
  62.             Toast.makeText(RemoteServiceBinding. this , R.string.remote_service_connected,   
  63.                     Toast.LENGTH_SHORT).show();   
  64.         }   
  65.   
  66.          public   void  onServiceDisconnected(ComponentName className) {   
  67.              // This is called when the connection with the service has been   
  68.              // unexpectedly disconnected -- that is, its process crashed.   
  69.             mService =  null ;   
  70.             mKillButton.setEnabled( false );   
  71.             mCallbackText.setText( "Disconnected." );   
  72.   
  73.              // As part of the sample, tell the user what happened.   
  74.             Toast.makeText(RemoteServiceBinding. this , R.string.remote_service_disconnected,   
  75.                     Toast.LENGTH_SHORT).show();   
  76.         }   
  77.     };   
  78.   
  79.      /**  
  80.      * Class for interacting with the secondary interface of the service.  
  81.      */   
  82.      private  ServiceConnection mSecondaryConnection =  new  ServiceConnection() {   
  83.          public   void  onServiceConnected(ComponentName className,   
  84.                 IBinder service) {   
  85.              // Connecting to a secondary interface is the same as any   
  86.              // other interface.   
  87.             mSecondaryService = ISecondary.Stub.asInterface(service);   
  88.             mKillButton.setEnabled( true );   
  89.         }   
  90.   
  91.          public   void  onServiceDisconnected(ComponentName className) {   
  92.             mSecondaryService =  null ;   
  93.             mKillButton.setEnabled( false );   
  94.         }   
  95.     };   
  96.   
  97.      private  OnClickListener mBindListener =  new  OnClickListener() {   
  98.          public   void  onClick(View v) {   
  99.              // Establish a couple connections with the service, binding   
  100.              // by interface names.  This allows other applications to be   
  101.              // installed that replace the remote service by implementing   
  102.              // the same interface.   
  103.             bindService( new  Intent(IRemoteService. class .getName()),   
  104.                     mConnection, Context.BIND_AUTO_CREATE);   
  105.             bindService( new  Intent(ISecondary. class .getName()),   
  106.                     mSecondaryConnection, Context.BIND_AUTO_CREATE);   
  107.             mIsBound =  true ;   
  108.             mCallbackText.setText( "Binding." );   
  109.         }   
  110.     };   
  111.   
  112.      private  OnClickListener mUnbindListener =  new  OnClickListener() {   
  113.          public   void  onClick(View v) {   
  114.              if  (mIsBound) {   
  115.                  // If we have received the service, and hence registered with   
  116.                  // it, then now is the time to unregister.   
  117.                  if  (mService !=  null ) {   
  118.                      try  {   
  119.                         mService.unregisterCallback(mCallback);   
  120.                     }  catch  (RemoteException e) {   
  121.                          // There is nothing special we need to do if the service   
  122.                          // has crashed.   
  123.                     }   
  124.                 }   
  125.   
  126.                  // Detach our existing connection.   
  127.                 unbindService(mConnection);   
  128.                 unbindService(mSecondaryConnection);   
  129.                 mKillButton.setEnabled( false );   
  130.                 mIsBound =  false ;   
  131.                 mCallbackText.setText( "Unbinding." );   
  132.             }   
  133.         }   
  134.     };   
  135.   
  136.      private  OnClickListener mKillListener =  new  OnClickListener() {   
  137.          public   void  onClick(View v) {   
  138.              // To kill the process hosting our service, we need to know its   
  139.              // PID.  Conveniently our service has a call that will return   
  140.              // to us that information.   
  141.              if  (mSecondaryService !=  null ) {   
  142.                  try  {   
  143.                      int  pid = mSecondaryService.getPid();   
  144.                      // Note that, though this API allows us to request to   
  145.                      // kill any process based on its PID, the kernel will   
  146.                      // still impose standard restrictions on which PIDs you   
  147.                      // are actually able to kill.  Typically this means only   
  148.                      // the process running your application and any additional   
  149.                      // processes created by that app as shown here; packages   
  150.                      // sharing a common UID will also be able to kill each   
  151.                      // other"s processes.   
  152.                     Process.killProcess(pid);   
  153.                     mCallbackText.setText( "Killed service process." );   
  154.                 }  catch  (RemoteException ex) {   
  155.                      // Recover gracefully from the process hosting the   
  156.                      // server dying.   
  157.                      // Just for purposes of the sample, put up a notification.   
  158.                     Toast.makeText(RemoteServiceBinding. this ,   
  159.                             R.string.remote_call_failed,   
  160.                             Toast.LENGTH_SHORT).show();   
  161.                 }   
  162.             }   
  163.         }   
  164.     };   
  165.   
  166.      // ----------------------------------------------------------------------   
  167.      // Code showing how to deal with callbacks.   
  168.      // ----------------------------------------------------------------------   
  169.   
  170.      /**  
  171.      * This implementation is used to receive callbacks from the remote  
  172.      * service.  
  173.      */   
  174.      private  IRemoteServiceCallback mCallback =  new  IRemoteServiceCallback.Stub() {   
  175.          /**  
  176.          * This is called by the remote service regularly to tell us about  
  177.          * new values.  Note that IPC calls are dispatched through a thread  
  178.          * pool running in each process, so the code executing here will  
  179.          * NOT be running in our main thread like most other things -- so,  
  180.          * to update the UI, we need to use a Handler to hop over there.  
  181.          */   
  182.          public   void  valueChanged( int  value) {   
  183.             mHandler.sendMessage(mHandler.obtainMessage(BUMP_MSG, value,  0 ));   
  184.         }   
  185.     };   
  186.   
  187.      private   static   final   int  BUMP_MSG =  1 ;   
  188.   
  189.      private  Handler mHandler =  new  Handler() {   
  190.          @Override   public   void  handleMessage(Message msg) {   
  191.              switch  (msg.what) {   
  192.                  case  BUMP_MSG:   
  193.                     mCallbackText.setText( "Received from service: "  + msg.arg1);   
  194.                      break ;   
  195.                  default :   
  196.                      super .handleMessage(msg);   
  197.             }   
  198.         }   
  199.   
  200.     };   
  201. }

抱歉!评论已关闭.