Server端:
1.添加与java同级的aidl文件夹,创建aidl接口类并在类中定义传递信息的接口函数(注:接口类所在包名要与实现类包名一致)
2.实现类实现aidl接口
public class MyService extends Service {
public MyService() {}
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
class MyBinder extends IMyAidlInterface.Stub{
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public String getName() throws RemoteException {
return “test”;
}
}
}
Client端:
1.复制导入和server端一致的aidl文件
2.绑定关联server端服务接口
IMyAidlInterface iMyAidlInterface
Intent intent = new Intent();
intent.setPackage(“com.example.aidlservice”);
intent.setAction(“com.example.aidlservice.MyService”);
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
},BIND_AUTO_CREATE);
3.使用关联的server端接口
iMyAidlInterface.getName()