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