在Android系统中,进程之间不能共享内存,为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Call,RPC)方式来实现。Android系统中提供了一种接口定义语言(Interface Definition Language,IDL)来公开服务的接口。而这种可以提供跨进程访问的服务称为AIDL(Android Interface Definition Language)服务。
在Android系统中,服务端通过实现AIDL接口来提供相应的功能服务,客户端连接到AIDL服务,调用相应的接口来实现远程功能调用。对于服务端的功能接口,客户端都是以远程调用的方式实现,因此,不通的客户端调用服务端,调用过程都不是在一个线程中完成的。下面介绍AIDL服务端和客户端的实现方式。
先看服务端实现过程,创建Android应用AidlServer
1.定义AIDL接口。
创建AIDL文件IAidlCommInterface.aidl,定义功能接口。
interface IAidlCommInterface {
void OnMessage(int msgId, int param1, int param2);
}
2.创建服务端服务,实现IAidlCommInterface.Stub接口。
public class TestService extends Service {
private AIDLServerBinder mAidlServerBinder = null;
@Override
public IBinder onBind(Intent intent) {
if (mAidlServerBinder == null){
mAidlServerBinder = new AIDLServerBinder();
}
return mAidlServerBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.e(" [TestService] ", "onCreate");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(" [TestService] ", "onDestroy");
}
public class AIDLServerBinder extends IAidlCommInterface.Stub{
@Override
public void OnMessage(int msgId, int param1, int param2) throws RemoteException {
Log.e(" [TestService] ", "OnMessage: " + msgId + "(" + param1 + "," + param2 + ")");
}
}
}
3.在AndroidManifest.xml添加intent-filter
<service
android:name=".TestService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.test.aidlserver.TestService" />
</intent-filter>
</service>
客户端实现过程如下:
创建客户端应用AidlClien
1.定义AIDL接口,使用与AidlServer相同的接口定义文件IAidlCommInterface.aidl
interface IAidlCommInterface {
void OnMessage(int msgId, int param1, int param2);
}
2.在Activity中连接AIDL服务并调用相应服务接口。服务端通过bindService来绑定AIDL服务,绑定成功后会回掉onServiceConnected,在onServiceConnected中会传递远程AIDL服务的IBinder引用,通过IAidlCommInterface.Stub.asInterface来转为IAidlCommInterface接口引用。这样就可以通过该接口直接进行远程功能调用。
public class MainActivity extends AppCompatActivity {
private IAidlCommInterface mAIDLService = null;
private ServiceConnection mServerConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.e(" [AidlClient] ", "onServiceConnected");
mAIDLService = IAidlCommInterface.Stub.asInterface(iBinder);
try {
mAIDLService.OnMessage(999, 111, 222);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mAIDLService = null;
Log.e(" [AidlClient] ", "onServiceDisconnected");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent("com.test.aidlserver.TestService");
this.bindService(intent, mServerConn, Context.BIND_AUTO_CREATE);
Log.e(" [AidlClient] ", "onStart");
}
}
以上的例子中,service端实现AIDL接口,并提供了系统服务,客户端通过bindService绑定到远程服务(注意,绑定时使用的intent的action必需与service端AndroidManifest.xml中对于服务的intent-filter标签中的action一致),连接成功后,客户端获取到一个远程AIDL服务接口对象,这样,客户端就可以调用服务端aidl接口
void OnMessage(int msgId, int param1, int param2);
来实现远程功能调用了。