同一工程下创建两个model (client端 server端)
一,server端
1、在main下创建adil文件(ServerAL)
interface ServerAL {
String getValue();
}
2、创建服务端service
public class BeheadService extends Service {
public BeheadService() {
}
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
public class MyBinder extends ServerAL.Stub {
@Override
public String getValue() throws RemoteException {
return "server";
}
}
;
}
3,注册server
<service
android:name=".BeheadService"
android:enabled="true"
android:exported="true"
//声明远程服务
android:process=":remote">
<intent-filter>
//隐式注册
<action android:name="com.server.aidl.behead" />
</intent-filter>
</service>
4,把整个aidl目录copy到client端的目录下
二、client编码
一,注册service
Intent intent = new Intent();
intent.setAction("com.server.aidl.behead");
//android 5.0之后禁止隐式注册service,特此加上包名
intent.setPackage("com.server.aidl");
bindService(intent, new MyConnection(), BIND_AUTO_CREATE);
2、获取service数据
class MyConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
ServerAL serverAL=ServerAL.Stub.asInterface(service);
try {
Log.e("tag", serverAL.getValue());
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}