本文共 2139 字,大约阅读时间需要 7 分钟。
aidl是 Android Interface definition language 是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口 (1)在 src文件夹下
创建 .aidl文件,在aidl文件定义如下:写法跟java代码类似 注:1> 它可以引用其它aidl文件中定义的接口,但是不能够引用你的java类文件中定义的接口 package com.epsot.mobile.android.group.driver.aidl; interface IRemoteGroupService { (2)编译aidl文件,这个只要是在eclipse中开发,你的adt插件会像资源文件一样把aidl文件编译成java代码生成在gen文件夹下,不用手动去编译:编译生成 IRemoteGroupService .java如我例子中代码 (3) 实现定义aidl接口中的内部抽象类 Stub, Stub类继承了Binder,并继承我们在aidl文件中定义的接口,我们需要实现接口方法,下面是我在例子中实现的Stub类: public class RemoteGroupService extends Service public IBinder onBind(Intent intent) return new RemoteGroupServiceImpl(); private class RemoteGroupServiceImpl extends IRemoteGroupService.Stub public String onGetGroupName() throws RemoteException return GroupApplication. currentGroupName ; public void onPlayCompletion() throws RemoteException public void onRecordVoice() throws RemoteException < service android:name = "com.epsot.mobile.android.group.driver.aidl.RemoteGroupService" > < action android:name = "com.epsot.mobile.android.group.driver.aidl.RemoteGroupService" /> < action android:name = "com.epsot.mobile.android.group.driver.aidl.RemoteGroupServiceMine" /> private IRemoteGroupService mRemoteGroupService; // 在合适的地方创建绑定服务,一般是在 onCreate 方法中 bindService( new Intent("com.epsot.mobile.android.group.driver.aidl.RemoteGroupService" ), mRemoteNavigationServiceConntection , Context.BIND_AUTO_CREATE ); private ServiceConnection mRemoteNavigationServiceConntection = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder service) { mRemoteGroupService = IRemoteGroupService.Stub.asInterface(service); public void onServiceDisconnected(ComponentName name) { mRemoteGroupService = null ; 注:在同一个App中 IRemoteGroupService 可以找到,在不同的App则需要拥有相同路径名的aidl文件