AIDL机制

前段时间研究了不少android二次开发,其中有一种方法就是通过aidl通信,留接口提供给外面二次开发。从这里也可以看出:aidl通信是两个应用程序之间的进程通信了。在这篇博客中,主要写了两个应用程序,一个是serverdemo,可以称为服务端,也是提供接口的应用程序,在这里面我写了一个加法计算。二是客户端:clientdemo,在这个程序中调用了加法计算接口,把值传到serverdemo进行加法计算,返回结果,进行显示。

1、aidl的定义

aidl是 Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口;icp:interprocess communication :内部进程通信。

2、两个项目结构以及实现效果

 

 

从上面图中看以大概看出,服务端布局什么都没有,不过这里面有加法计算的服务。而客户端有两个输入框输入两个值,点击计算。

3、server服务端

3.1、新建创建你的aidl文件

保存你的aidl文件,这个只要是在eclipse中开发,你的adt插件会像资源文件一样把aidl文件编译成java代码生成在gen文件夹下,不用手动去编译:编译生成AIDLService.java如我例子中代码。

IBoardADDInterface.aidl

[java]  view plain  copy
  1. package com.example.server;  
  2. import android.os.Bundle;  
  3.   
  4. /*** 
  5.  * System private API for talking with the caculate service. 
  6.  * 
  7.  * {@hide} 
  8.  */  
  9. interface IBoardADDInterface  
  10. {   
  11.     int add(int nValue1,int nValue2);  
  12. }  
自动把 aidl文件编译成java代码生成在gen文件夹下IBoardADDInterface的接口代码
[java]  view plain  copy
  1. /* 
  2.  * This file is auto-generated.  DO NOT MODIFY. 
  3.  * Original file: C:\\Users\\southgnssliyc\\Desktop\\android aidl\\ServerDemo\\src\\com\\example\\server\\IBoardADDInterface.aidl 
  4.  */  
  5. package com.example.server;  
  6. /*** 
  7.  * System private API for talking with the caculate service. 
  8.  * 
  9.  * {@hide} 
  10.  */  
  11. public interface IBoardADDInterface extends android.os.IInterface  
  12. {  
  13. /** Local-side IPC implementation stub class. */  
  14. public static abstract class Stub extends android.os.Binder implements com.example.server.IBoardADDInterface  
  15. {  
  16. private static final java.lang.String DESCRIPTOR = "com.example.server.IBoardADDInterface";  
  17. /** Construct the stub at attach it to the interface. */  
  18. public Stub()  
  19. {  
  20. this.attachInterface(this, DESCRIPTOR);  
  21. }  
  22. /** 
  23.  * Cast an IBinder object into an com.example.server.IBoardADDInterface interface, 
  24.  * generating a proxy if needed. 
  25.  */  
  26. public static com.example.server.IBoardADDInterface asInterface(android.os.IBinder obj)  
  27. {  
  28. if ((obj==null)) {  
  29. return null;  
  30. }  
  31. android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);  
  32. if (((iin!=null)&&(iin instanceof com.example.server.IBoardADDInterface))) {  
  33. return ((com.example.server.IBoardADDInterface)iin);  
  34. }  
  35. return new com.example.server.IBoardADDInterface.Stub.Proxy(obj);  
  36. }  
  37. @Override public android.os.IBinder asBinder()  
  38. {  
  39. return this;  
  40. }  
  41. @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException  
  42. {  
  43. switch (code)  
  44. {  
  45. case INTERFACE_TRANSACTION:  
  46. {  
  47. reply.writeString(DESCRIPTOR);  
  48. return true;  
  49. }  
  50. case TRANSACTION_add:  
  51. {  
  52. data.enforceInterface(DESCRIPTOR);  
  53. int _arg0;  
  54. _arg0 = data.readInt();  
  55. int _arg1;  
  56. _arg1 = data.readInt();  
  57. int _result = this.add(_arg0, _arg1);  
  58. reply.writeNoException();  
  59. reply.writeInt(_result);  
  60. return true;  
  61. }  
  62. }  
  63. return super.onTransact(code, data, reply, flags);  
  64. }  
  65. private static class Proxy implements com.example.server.IBoardADDInterface  
  66. {  
  67. private android.os.IBinder mRemote;  
  68. Proxy(android.os.IBinder remote)  
  69. {  
  70. mRemote = remote;  
  71. }  
  72. @Override public android.os.IBinder asBinder()  
  73. {  
  74. return mRemote;  
  75. }  
  76. public java.lang.String getInterfaceDescriptor()  
  77. {  
  78. return DESCRIPTOR;  
  79. }  
  80. @Override public int add(int nValue1, int nValue2) throws android.os.RemoteException  
  81. {  
  82. android.os.Parcel _data = android.os.Parcel.obtain();  
  83. android.os.Parcel _reply = android.os.Parcel.obtain();  
  84. int _result;  
  85. try {  
  86. _data.writeInterfaceToken(DESCRIPTOR);  
  87. _data.writeInt(nValue1);  
  88. _data.writeInt(nValue2);  
  89. mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);  
  90. _reply.readException();  
  91. _result = _reply.readInt();  
  92. }  
  93. finally {  
  94. _reply.recycle();  
  95. _data.recycle();  
  96. }  
  97. return _result;  
  98. }  
  99. }  
  100. static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);  
  101. }  
  102. public int add(int nValue1, int nValue2) throws android.os.RemoteException;  
  103. }  
这代码一看就是自动生成的。
3.2、服务端的计算加法实现类,写一个server

[java]  view plain  copy
  1. package com.example.server;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.IBinder;  
  6. import android.os.RemoteException;  
  7. import android.util.Log;  
  8.   
  9. /** 
  10.  * 服务端的计算加法实现类 
  11.  * @author mmsx 
  12.  * 
  13.  */  
  14. public class CaculateAddService extends Service {  
  15.     //加法计算的服务  
  16.     final String CACULATE_ADD = "COM.CACULATE.ADD";  
  17.   
  18.     //找到自定义服务  
  19.     @Override  
  20.     public IBinder onBind(Intent intent) {    
  21.         if(intent.getAction().equals(CACULATE_ADD))  
  22.         {  
  23.             return mIBinder_CACULATE_ADD;  
  24.         }  
  25.         return null;  
  26.     }  
  27.   
  28.     @Override  
  29.     public boolean onUnbind(Intent intent) {  
  30.         return super.onUnbind(intent);  
  31.     }  
  32.   
  33.     @Override  
  34.     public void onDestroy() {  
  35.         super.onDestroy();  
  36.     }  
  37.   
  38.     @Override  
  39.     public void onCreate() {  
  40.         super.onCreate();  
  41.     }  
  42.   
  43.     //aidl的接口实现  
  44.     private final IBinder mIBinder_CACULATE_ADD = new IBoardADDInterface.Stub()   
  45.     {  
  46.   
  47.         @Override  
  48.         public int add(int nValue1, int nValue2) throws RemoteException {  
  49.             Log.i("Show", String.valueOf(nValue1) + ",,," +String.valueOf(nValue2));  
  50.             return nValue1 + nValue2;  
  51.         }  
  52.           
  53.     };    
  54. }  

既然你写了一个service,那么就要在AndroidManifest.xml中添加注册

[html]  view plain  copy
  1. <service android:name="com.example.server.CaculateAddService" >  
  2.     <intent-filter>  
  3.         <action android:name="COM.CACULATE.ADD" >  
  4.         </action>  
  5.     </intent-filter>  
  6. </service>  
这个名称是自定义的:COM.CACULATE.ADD。service的路径com.example.server.CaculateAddService。

到这里就写完了这个服务端的应用程序,是不是很简单。activity都没写什么,因为只是用到里面的一个service和aidl。

4、客户端client

4.1、把服务端的aidl拷到客户端,代码不变

[java]  view plain  copy
  1. package com.example.server;  
  2. import android.os.Bundle;  
  3.   
  4. /*** 
  5.  * System private API for talking with the caculate service. 
  6.  * 
  7.  * {@hide} 
  8.  */  
  9. interface IBoardADDInterface  
  10. {   
  11.     int add(int nValue1,int nValue2);  
  12. }  
自动编译生成的代码就不贴了。
.4.2、写一个绑定计算服务的类CaculateManager

[java]  view plain  copy
  1. package com.example.clientdemo;  
  2.   
  3. import com.example.server.IBoardADDInterface;  
  4.   
  5. import android.content.ComponentName;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.content.ServiceConnection;  
  9. import android.os.IBinder;  
  10.   
  11. /** 
  12.  * 客户端的服务计算管理类 
  13.  * @author mmsx 
  14.  * 
  15.  */  
  16. public class CaculateManager {  
  17.     //加法计算的服务  
  18.     final String CACULATE_ADD = "COM.CACULATE.ADD";  
  19.   
  20.     //aidi接口服务  
  21.     IBoardADDInterface mService = null;  
  22.       
  23.     /*** 
  24.      * 服务绑定 
  25.      */  
  26.     public void bindService(Context context) {  
  27.         mContext = context;   
  28.         context.bindService(new Intent(CACULATE_ADD),  
  29.                 serviceConnection, Context.BIND_AUTO_CREATE);  
  30.     }  
  31.       
  32.     Context mContext = null;  
  33.       
  34.     /*** 
  35.     * 解除服务绑定 
  36.     */  
  37.     public void unbindService()  
  38.     {  
  39.         if (mContext != null) {  
  40.             mContext.unbindService(serviceConnection);  
  41.         }  
  42.     }  
  43.       
  44.     /** 
  45.      * 加法计算 
  46.      * @param nValue1 
  47.      * @param nValue2 
  48.      * @return 结果 
  49.      */  
  50.     public int caculateAdd(int nValue1,int nValue2)  
  51.     {  
  52.         if (mService == null)  
  53.             return 0;  
  54.   
  55.         try {  
  56.             return mService.add(nValue1, nValue2);  
  57.         } catch (Exception e) {  
  58.             return 0;  
  59.         }  
  60.     }  
  61.   
  62.       
  63.     //服务和aidl接口绑定  
  64.     private ServiceConnection serviceConnection = new ServiceConnection() {  
  65.   
  66.         @Override  
  67.         public void onServiceDisconnected(ComponentName name) {  
  68.             mService = null;  
  69.         }  
  70.   
  71.         @Override  
  72.         public void onServiceConnected(ComponentName name, IBinder service) {  
  73.             mService = IBoardADDInterface.Stub.asInterface(service);      
  74.         }  
  75.     };  
  76. }  
这里面有找到服务,解除服务。方法实现的接口。
4.3、activity输入数据,调用接口

[java]  view plain  copy
  1. package com.example.clientdemo;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.EditText;  
  8. import android.widget.TextView;  
  9.   
  10. public class MainActivity extends Activity {  
  11.     CaculateManager caculateManager = new CaculateManager();  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.           
  17.         caculateManager.bindService(this);  
  18.         findViewById(R.id.button1).setOnClickListener(new OnClickListener() {  
  19.               
  20.             @Override  
  21.             public void onClick(View v) {  
  22.                 EditText editText1 = (EditText)findViewById(R.id.editText1);  
  23.                 EditText editText2 = (EditText)findViewById(R.id.editText2);  
  24.                 int nValue1 = Integer.parseInt(editText1.getText().toString().trim());  
  25.                 int nValue2 = Integer.parseInt(editText2.getText().toString().trim());  
  26.                 int nResult = caculateManager.caculateAdd(nValue1, nValue2);  
  27.                   
  28.                 TextView textView = (TextView)findViewById(R.id.textView1);  
  29.                 textView.setText("计算结果:" + String.valueOf(nResult));  
  30.             }  
  31.         });  
  32.     }  
  33.   
  34. }  
xml代码很简单

[html]  view plain  copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/LinearLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context=".MainActivity" >  
  8.   
  9.     <TextView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="使用aidi服务调用其他程序计算,返回结果" />  
  13.   
  14.     <EditText  
  15.         android:id="@+id/editText1"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:ems="10"  
  19.         android:inputType="numberDecimal" >  
  20.   
  21.         <requestFocus />  
  22.     </EditText>  
  23.   
  24.     <EditText  
  25.         android:id="@+id/editText2"  
  26.         android:layout_width="match_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:ems="10"  
  29.         android:inputType="numberDecimal" />  
  30.   
  31.     <TextView  
  32.         android:id="@+id/textView1"  
  33.         android:layout_width="wrap_content"  
  34.         android:layout_height="wrap_content"  
  35.         android:text="计算结果:" />  
  36.   
  37.     <Button  
  38.         android:id="@+id/button1"  
  39.         android:layout_width="match_parent"  
  40.         android:layout_height="wrap_content"  
  41.         android:text="加法计算" />  
  42.   
  43. </LinearLayout>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值