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

android进程间通信:使用AIDL

2013年10月07日 ⁄ 综合 ⁄ 共 4061字 ⁄ 字号 评论关闭

   《!-- 之前的Android底层部分,进程间通信Binder(IPC)部分。Binder我到现在还是有点懵懵懂懂的--!微笑括号内为我自己写的感受,以下是相关博主的文章》

     欢迎阅读本文,你能关注本文,你知道你需要进程间通信、需要AIDL(以及Binder),那么可以默认你对这些概念已经有了一些了解,你(大致)知道它们是什么,它们有什么用,所以为了节约大家的眼力和时间,在此我不复制粘贴网上泛滥的博客或者翻译冗长的android文档。

      关于AIDL的介绍在文档:docs/guide/developing/tools/aidl.html

      关于IBinder的介绍在文档:docs/reference/android/os/IBinder.html

      以及Binder:docs/reference/android/os/Binder.html

      在后文中,我将以我自己的理解向你介绍相关的概念。以我目前粗浅的经验,应用程序使用AIDL的地方,几乎都和Service有关,所以你也需要知道一些关于Service的知识。日后得闲我也会继续写一些关于Service的贴。

      本文将以一个例子来和你分享使用AIDL的基础技能,这个例子里有:

1、一个类mAIDLActivity,继承Activity。里面有三个按钮,text分别为StartService,StopService,CallbackTest。

2、一个类mAIDLService,继承Service。为了充分展示ADIL的功能,它做以下工作:当用户点击CallbackTest按钮时,从mAIDLActivity调用mAIDLService中的Stub 对象的一个方法invokCallBack(),而这个方法又会调用mAIDLActivity中Stub 对象的一个方法performAction(),这个方法在屏幕上显示一个toast。没什么意义,只是展示一下AIDL如何使用。

3、两个AIDL文件:forService.aidl和forActivity.aidl。对应名字,在Service和Activity中分别有对象需要用到它们定义的接口。

4、相关XML文件,略过。关于manifest中Service的语法,见docs/guide/topics/manifest/service-element.html。你也可以简单地在<application></application>中加入

     <service android:name=".mAIDLService" android:process=":remote"> </service>

开发环境为Eclipse。

拣重要的先说,来看看aidl文件的内容:

文件:forActivity.aidl

[java] view
plain
copy

  1. package com.styleflying.AIDL;  
  2. interface forActivity {  
  3.     void performAction();  
  4.     }  

 

文件:forService.aidl

[java] view
plain
copy

  1. package com.styleflying.AIDL;  
  2. import com.styleflying.AIDL.forActivity;  
  3. interface forService {  
  4.     void registerTestCall(forActivity cb);  
  5.     void invokCallBack();  
  6. }  

 

这两个文件和Java文件放置的地方一样,看包名。

在Eclipse中它们将被自动编译为forActivity.java和forService.java,它们存放在gen目录下。为了方便手头无法演练的读者,代码贴上,不用细看。

文件forActivity.java:

[java] view
plain
copy

  1. /* 
  2.  * This file is auto-generated.  DO NOT MODIFY. 
  3.  * Original file: D://workspace//AIDLTest//src//com//styleflying//AIDL//forActivity.aidl 
  4.  */  
  5. package com.styleflying.AIDL;  
  6. import java.lang.String;  
  7. import android.os.RemoteException;  
  8. import android.os.IBinder;  
  9. import android.os.IInterface;  
  10. import android.os.Binder;  
  11. import android.os.Parcel;  
  12. public interface forActivity extends android.os.IInterface  
  13. {  
  14. /** Local-side IPC implementation stub class. */  
  15. public static abstract class Stub extends android.os.Binder implements com.styleflying.AIDL.forActivity  
  16. {  
  17. private static final java.lang.String DESCRIPTOR = "com.styleflying.AIDL.forActivity";  
  18. /** Construct the stub at attach it to the interface. */  
  19. public Stub()  
  20. {  
  21. this.attachInterface(this, DESCRIPTOR);  
  22. }  
  23. /** 
  24.  * Cast an IBinder object into an forActivity interface, 
  25.  * generating a proxy if needed. 
  26.  */  
  27. public static com.styleflying.AIDL.forActivity asInterface(android.os.IBinder obj)  
  28. {  
  29. if ((obj==null)) {  
  30. return null;  
  31. }  
  32. android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);  
  33. if (((iin!=null)&&(iin instanceof com.styleflying.AIDL.forActivity))) {  
  34. return ((com.styleflying.AIDL.forActivity)iin);  
  35. }  
  36. return new com.styleflying.AIDL.forActivity.Stub.Proxy(obj);  
  37. }  
  38. public android.os.IBinder asBinder()  
  39. {  
  40. return this;  
  41. }  
  42. @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException  
  43. {  
  44. switch (code)  
  45. {  
  46. case INTERFACE_TRANSACTION:  
  47. {  
  48. reply.writeString(DESCRIPTOR);  
  49. return true;  
  50. }  
  51. case TRANSACTION_performAction:  
  52. {  
  53. data.enforceInterface(DESCRIPTOR);  
  54. this.performAction();  
  55. reply.writeNoException();  
  56. return true;  
  57. }  
  58. }  
  59. return super.onTransact(code, data, reply, flags);  
  60. }  
  61. private static class Proxy implements com.styleflying.AIDL.forActivity  
  62. {  
  63. private android.os.IBinder mRemote;  
  64. Proxy(android.os.IBinder remote)  
  65. {  
  66. mRemote = remote;  
  67. }  
  68. public android.os.IBinder asBinder()  
  69. {  
  70. return mRemote;  
  71. }  
  72. public java.lang.String getInterfaceDescriptor()  
  73. {  
  74. return DESCRIPTOR;  
  75. }  
  76. public void performAction() throws android.os.RemoteException  
  77. {  
  78. android.os.Parcel _data = android.os.Parcel.obtain();  
  79. android.os.Parcel _reply = android.os.Parcel.obtain();  
  80. try {  
  81. _data.writeInterfaceToken(DESCRIPTOR);  
  82. mRemote.transact(Stub.TRANSACTION_performAction, _data, _reply, 0);  
  83. _reply.readException();  
  84. }  
  85. finally {  

抱歉!评论已关闭.