AIDL (Android Interface Definition Language) 是一種IDL 語言,用于生成可以在Android裝置上兩個程序之間進行程序間通信(interprocess communication, IPC)的代碼。如果在一個程序中(例如Activity)要調用另一個程序中(例如Service)對象的操作,就可以使用AIDL生成可序列化的參數。本文簡單介紹AIDL的使用。
1.建立IRemoteService.aidl
package com.tang.remoteservicedemo;
interface IRemoteService {
String getInfo();
}
從内容中也可以看出,這東西類似一個接口。既然定義了這麼一個玩意,那麼我們就要去實作它。
2.建立IService“實作”IRemoteService“接口”
package com.tang.remoteservicedemo;
import java.util.Date;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class IService extends Service
{
private IBinder iBinder = new IRemoteService.Stub() {
@Override
public String getInfo() throws RemoteException {
// TODO Auto-generated method stub
return new Date(System.currentTimeMillis()).toLocaleString()+" 來自遠端服務的資訊!";
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return iBinder;
}
}
基于 Binder的不同程序間通信, Client與Service在不同的程序中,對使用者程式而言當調用Service傳回的IBinder接口後,通路Service中的方法就如同調用自己的函數一樣。
3.配置IService供其他程序調用
<service android:name="com.tang.remoteservicedemo.IService"
android:process=":remote">
<intent-filter>
<action android:name="com.tang.remoteservicedemo.IService"/>
</intent-filter>
</service>
配置一個所屬程序名和一個action。
通過前面三個步驟,這個含有getInfo()方法的service就可以給别人調用了,下面在用戶端調用它
4.建立一個ClientDemo工程将含有IRemoteService.aidl的那個包全部拷貝到src下,隻留下aidl檔案,其他全删除。

5.建立MainActivity,其他就是綁定service的操作了
package com.tang.clientdemo;
import java.util.Timer;
import java.util.TimerTask;
import com.tang.remoteservicedemo.IRemoteService;
import android.app.Activity;
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.util.Log;
public class MainActivity extends Activity {
private IRemoteService iService = null;
private boolean isBinded =false;
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
isBinded = false;
iService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
iService = IRemoteService.Stub.asInterface(service);
isBinded = true;
}
};
public void doBind()
{
Intent intent = new Intent("com.tang.remoteservicedemo.IService");
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
public void doUnbind()
{
if (isBinded)
{
unbindService(conn);
iService = null;
isBinded = false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
doBind();
}
}).start();
Timer timer = new Timer();
timer.schedule(task, 0, 2000);
}
TimerTask task = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
if(iService!=null)
{
try {
Log.i("AAA", iService.getInfo());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
Log.i("AAA", "iService!=null");
}
}
};
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
doUnbind();
task.cancel();
super.onDestroy();
}
}
執行之後的Log如下:
為什麼會有一個為空的Log呢,因為綁定也是要時間的嘛....
簡單的AIDL的使用就這麼多了
源碼下載下傳