使用AIDL实现IPC 服务的步骤是:
1、首先创建aidl文件,该文件(YourInterface.aidl)定义了客户端可用的方法和数据的接口。它的定义方法与接口是一样的;(会在gen目录下面自动生成对应的YourInterface .java文件)
IRemoteService.aidl
package hb.android.aidl;
interface IRemoteService {
int getPid();
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}
IRemoteService2.aidl
package hb.android.aidl;
interface IRemoteService2{
void sysout();
String returnStr();
}
自动生成的java文件如图:
2、 实现接口-AIDL编译器从AIDL接口文件中利用Java语言创建接口,该接口有一个继承的命名为Stub的内部抽象类(并且实现了一些IPC 调用的附加方法),要做的就是创建一个继承于YourInterface.Stub 的类并且实现在.aidl 文件中声明的方法。(一般是在服务中实现这个接口,在服务中使用IPC)
/**
- 实现定义的aidl接口。
*/
private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public int getPid() {
return Process.myPid();
}
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, Str