AIDL和其他的IDL类似,它允许你定义程序接口,以便客户端与服务器端通过IPC机制交互。在android上面,一个进程一般不能访问另外进程的内存。因此,Android平台将这些跨进程访问的对象分解成操作系统能够识别的简单对象。并为跨应用访问而特殊编排和整理这些对象。用于编排和整理这些对象的代码编写起来十分冗长,所以Android的AIDL提供了相关工具来自动生成这些代码。今天,我们开始AIDL的学习。
一,AIDL的定义:
它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口
二、AIDL的开发流程:
- 定义AIDL文件(先在服务端定义,客户端也要定义,内容跟服务端一样)
- 服务端实现接口
- 客户端调用接口
具体的aidl的说明: http://www.cnblogs.com/popapa/p/android_aidl.html
AIDL的简单使用之定义 AIDL 接口
为了测试,我写了aidl服务器端和aidl客户端!
aidl服务器端
项目结构如下:
一、 创建.aidl文件:IRemoteAIDL.aidl
// IRemoteAIDL.aidl package com.huhx.linux; // Declare any non-default types here with import statements interface IRemoteAIDL { int add(int a, int b); }
二、 定义服务类:RemoteService.java
package com.huhx.linux.aidlserver; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import com.huhx.linux.IRemoteAIDL; public class RemoteService extends Service { private final static String TAG = "RemoteService"; private IRemoteAIDL.Stub binder = new IRemoteAIDL.Stub() { @Override public int add(int a, int b) throws RemoteException { Log.i(TAG, "a = " + a + ", b = " + b); return a + b; } }; @Override public IBinder onBind(Intent intent) { return binder; } }
三、 在AndroidManifest.xml文件中声明服务:
<service android:name=".RemoteService" android:enabled="true" android:exported="true" />
aidl客户端
项目结构如下:
一、 创建与服务器端一样的aidl文件: IRemoteAIDL.aidl
// IRemoteAIDL.aidl package com.huhx.linux; // Declare any non-default types here with import statements interface IRemoteAIDL { int add(int a, int b); }
二、 在MainActivity中,得到远程服务并调用它的方法。
package com.huhx.linux.aidlclient; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; import com.huhx.linux.IRemoteAIDL; public class MainActivity extends AppCompatActivity { private IRemoteAIDL iRemoteAIDL; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); binServices(); } private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { iRemoteAIDL = IRemoteAIDL.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { iRemoteAIDL = null; } }; // 绑定服务 private void binServices() { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.huhx.linux.aidlserver", "com.huhx.linux.aidlserver.RemoteService")); bindService(intent, conn, Context.BIND_AUTO_CREATE); } // 处理相加的方法 public void addMethod(View view) { try { int result = iRemoteAIDL.add(3, 4); Toast.makeText(MainActivity.this, "3 + 4 = " + result, Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { e.printStackTrace(); } } }
三、 根据aidl文件,android生成的接口如下(可以不关注):
/* * This file is auto-generated. DO NOT MODIFY. * Original file: J:\\android\\andridtest\\andridtest\\2016-02-29\\BaseTest8\\aidlclient\\src\\main\\aidl\\com\\huhx\\linux\\IRemoteAIDL.aidl */ package com.huhx.linux; // Declare any non-default types here with import statements public interface IRemoteAIDL extends android.os.IInterface { /** Local-side IPC implementation stub class. */ public static abstract class Stub extends android.os.Binder implements com.huhx.linux.IRemoteAIDL { private static final java.lang.String DESCRIPTOR = "com.huhx.linux.IRemoteAIDL"; /** Construct the stub at attach it to the interface. */ public Stub() { this.attachInterface(this, DESCRIPTOR); } /** * Cast an IBinder object into an com.huhx.linux.IRemoteAIDL interface, * generating a proxy if needed. */ public static com.huhx.linux.IRemoteAIDL asInterface(android.os.IBinder obj) { if ((obj==null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (((iin!=null)&&(iin instanceof com.huhx.linux.IRemoteAIDL))) { return ((com.huhx.linux.IRemoteAIDL)iin); } return new com.huhx.linux.IRemoteAIDL.Stub.Proxy(obj); } @Override public android.os.IBinder asBinder() { return this; } @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException { switch (code) { case INTERFACE_TRANSACTION: { reply.writeString(DESCRIPTOR); return true; } case TRANSACTION_add: { data.enforceInterface(DESCRIPTOR); int _arg0; _arg0 = data.readInt(); int _arg1; _arg1 = data.readInt(); int _result = this.add(_arg0, _arg1); reply.writeNoException(); reply.writeInt(_result); return true; } } return super.onTransact(code, data, reply, flags); } private static class Proxy implements com.huhx.linux.IRemoteAIDL { private android.os.IBinder mRemote; Proxy(android.os.IBinder remote) { mRemote = remote; } @Override public android.os.IBinder asBinder() { return mRemote; } public java.lang.String getInterfaceDescriptor() { return DESCRIPTOR; } @Override public int add(int a, int b) throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); int _result; try { _data.writeInterfaceToken(DESCRIPTOR); _data.writeInt(a); _data.writeInt(b); mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0); _reply.readException(); _result = _reply.readInt(); } finally { _reply.recycle(); _data.recycle(); } return _result; } } static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); } public int add(int a, int b) throws android.os.RemoteException; }