app進程間通信,會用到AIDL,下面舉例一個server和其他app之間關於AIDL使用的流程:
1、創建一個android項目,創建一個server類,類似於
myService extends Service{}
2、創建AIDL文件,ImyService.aidl、ImyServiceBackcall.aidl,backcall文件主要用於回訪,即服務器端的調用:
2.1 ImyServiceBackcall.aidl文件:
interface ImyServiceBackcall {
void mClient();
}
2.2 ImyService.aidl文件:
interface ImyService {
void add(int a);
//綁定server對client的回訪aidl
void registerCallback(ImyServiceBackcall cb);
void unregisterCallback(ImyServiceBackcall cb);
}
編寫完之後保存,會自動在項目的gen目錄生成得到ImyService.java、ImyServiceBackcall.java文件,java文件裏面包含了stub接口
3、編譯stub子類,這個類就是對接口函數的實現,比如:
ServiceBinder extends ImyService.Stub {
<span style="white-space:pre"> </span>myService server;
void add(){
server.XXX();
}
}
4、外部app對aidl接口的調用
ImyService mServer;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mService = <span style="font-family: Arial, Helvetica, sans-serif;">ImyService </span>.Stub.asInterface(service);
try {
mService.registerCallback(mCallback);
} catch (RemoteException e) {
}
}
public void onServiceDisconnected(ComponentName className) {
mService = null;
}
//客戶端實現,用於服務器端回調使用
private ImyServiceBackcall mCallback = new ImyServiceBackcall.Stub() {
public void mClient() {
printf("client");
}
};
};
</pre><pre name="code" class="java">參考網絡文獻: http://blog.csdn.net/songjinshi/article/details/22918405