AIDL是 Android Interface definition language的缩写,是android内部进程通信接口的描述语言,通过它我们可以进行进程间的通信。
AIDL使用步骤
使用AIDL一般是在两个应用的通信中使用。一个作为服务端提供接口,一个作为客户端调用接口。
使用步骤
1.在服务端定义自定义类型 注意需要实现Parcelable 接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| public class Car implements Parcelable {
public Car() { }
private int prise;
public int getPrise() { return prise; }
public void setPrise(int prise) { this.prise = prise; }
protected Car(Parcel in) { this.prise=in.readInt();//这两个方法一定要重写,不然传递的数据为空 }
@Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(this.prise);//这两个方法一定要重写,不然传递的数据为空 }
@Override public int describeContents() { return 0; }
public static final Creator<Car> CREATOR = new Creator<Car>() { @Override public Car createFromParcel(Parcel in) { return new Car(in); }
@Override public Car[] newArray(int size) { return new Car[size]; } }; }
|
2.在服务端定义Car.aidl
1 2
| package com.orange.aidl; parcelable Car;
|
创建时会提示接口名字要唯一,所以我先创建一个其他名字的,在修改名字为car
3.在服务端定义IMyAidlInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import com.orange.aidl.Car;
interface IMyAidlInterface { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */
int getCarPrise(in Car car);
void setCar(in Car car);
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); }
|
4.在服务端创建并定义Service,同时在AndroidManifest中定义Service
1 2
| android:name=".MyService" android:exported="true"></service>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| public class MyService extends Service {
public MyService() { }
@Override public IBinder onBind(Intent intent) { return iBinder; }
Car carl;
private IBinder iBinder= new IMyAidlInterface.Stub()
{ @Override public int getCarPrise(Car car) throws RemoteException { if(carl!=null){ return carl.getPrise(); }else{ return 0; } }
@Override public void setCar(Car car) throws RemoteException { carl=car; }
@Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
} };
}
|
5.在客户端中重复前三步,其中名字都要一样
6.在客户端中绑定服务端的服务
1 2 3 4 5
| private void bindService(){ Intent intent=new Intent(); intent.setComponent(new ComponentName("com.orange.aidl","com.orange.aidl.MyService")); bindService(intent,connection, Context.BIND_AUTO_CREATE); }
|
7.在绑定服务后调用服务端的接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| private ServiceConnection connection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { TextView textView= (TextView) findViewById(R.id.tv); iMyAidlInterface=IMyAidlInterface.Stub.asInterface(service); Car car=new Car(); car.setPrise(1245633); try { iMyAidlInterface.setCar(car); int prise= iMyAidlInterface.getCarPrise(car); textView.setText(prise+""); } catch (RemoteException e) { e.printStackTrace(); } }
@Override public void onServiceDisconnected(ComponentName name) { iMyAidlInterface = null; } };
|
demo地址
github地址