创建.aildl 文件
具体的业务对象实现基于 .aidl 文件生成的接口
向客户端公开接口
客户端远程调用
验证 AIDL
创建.aildl 文件
java 的 8 种数据类型:byte、short、int、long、float、double、boolean、char
除此之外支持 String、charSequence、List、Map
自定义数据类型
package com.manu.aidldemo;
interface IPersonAidlInterface {
void setName(String name);
void setAge(int age);
String getInfo();
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt,long aLong,boolean aBoolean,float aFloat,double aDouble,String aString);
}
具体的业务对象实现基于 .aidl 文件生成的接口
public static abstract class Stub extends android .os .Binder implements com .manu .aidldemo .IPersonAidlInterface
public static com.manu.aidldemo.IPersonAidlInterface asInterface (android.os.IBinder obj){
if ((obj==null )) {
return null ;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null )&&(iin instanceof com.manu.aidldemo.IPersonAidlInterface))) {
return ((com.manu.aidldemo.IPersonAidlInterface)iin);
}
return new com.manu.aidldemo.IPersonAidlInterface.Stub.Proxy(obj);
}
public class Binder implements IBinder {
}
@Override
public void setName (java.lang.String name) throws android.os.RemoteException{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(name);
mRemote.transact(Stub.TRANSACTION_setName,_data,_reply,0 );
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
@Override
public boolean onTransact (int code,android.os.Parcel data,android.os.Parcel reply,int flags) throws android.os.RemoteException{
switch (code){
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true ;
}
case TRANSACTION_setName:
{
this .setName(_arg0);
}
}
}
/**
* Created by jzman
* Powered by 2018/3/8 0008.
*/
public class IPersonImpl extends IPersonAidlInterface .Stub {
private String name;
private int age;
@Override
public void setName (String name) throws RemoteException {
this .name = name;
}
@Override
public void setAge (int age) throws RemoteException {
this .age = age;
}
@Override
public String getInfo () throws RemoteException {
return "My name is " +name+",age is " +age+"!" ;
}
@Override
public void basicTypes (int anInt,String aString) throws RemoteException {
}
}
向客户端公开接口
public class PersonService extends Service {
public PersonService () {
}
@Override
public IBinder onBind (Intent intent) {
return new IPersonImpl();
}
}
<service
android:name =".PersonService"
android:enabled ="true"
android:exported ="true"
android:process =":remote" >
</service >
客户端远程调用
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity" ;
private IPersonAidlInterface iPersonAidlInterface;
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void bindServiceClick (View view) {
Log.i(TAG,"绑定服务..." );
Intent intent = new Intent(this ,PersonService.class);
bindService(intent,conn,Context.BIND_AUTO_CREATE);
}
public void unbindServiceClick (View view) {
Log.i(TAG,"解绑服务..." );
unbindService(conn);
}
public void callRemoteClick (View view) {
Log.i(TAG,"远程调用具体服务..." );
try {
iPersonAidlInterface.setName("Tom" );
iPersonAidlInterface.setAge(10 );
String info = iPersonAidlInterface.getInfo();
System.out.println("这是远程调用的服务信息:" +info);
} catch (RemoteException e) {
e.printStackTrace();
}
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected (ComponentName name,IBinder service) {
iPersonAidlInterface = IPersonAidlInterface.Stub.asInterface(service);
System.out.println("具体的业务对象:" +iPersonAidlInterface);
}
@Override
public void onServiceDisconnected (ComponentName name) {
}
};
}
验证 AIDL
1. 相同进程
2. 不同进程
最后