1.創建aidl文件
在android studio下 點擊項目 new–>AIDL–>aidl file
然後點擊 make project(ctrl + F9)後 會在 app\build\generated\source\aidl\debug\com\bskuai\aidltest\aidl*IMyAidlInterface.java*
IMyAidlInterface.java 文件就是ide幫我們自動生成的java文件
2.創建service
package com.bskuai.aidltest;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.bskuai.aidltest.aidl.IMyAidlInterface;
public class MyService extends Service {
private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public void add() throws RemoteException {
Log.e("tag","add");
}
@Override
public void delete() throws RemoteException {
Log.e("tag","delete");
}
};
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return mBinder;
}
}
3.綁定Service
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myAidlInterface = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
myAidlInterface = null;
}
};
Intent intent = new Intent(this,MyService.class);
this.bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
4.配置服務
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"
android:process=":remote"
>
</service>
android:process=”:remote” 讓服務運行在 包名:remote 的進程中
以上已經實現不同進程間的通信
5.瞭解自動生成的IMyAidlInterface.java
在客戶端中我們 使用了 myAidlInterface = IMyAidlInterface.Stub.asInterface(service);
asInterface()方法
public static com.bskuai.aidltest.aidl.IMyAidlInterface asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.bskuai.aidltest.aidl.IMyAidlInterface))) {
return ((com.bskuai.aidltest.aidl.IMyAidlInterface) iin);
}
return new com.bskuai.aidltest.aidl.IMyAidlInterface.Stub.Proxy(obj);
}
如果傳進來的obj 爲null 直接返回 null 通過 queryLocalInterface 得到IInterface對象 如果 該對象不爲null 並且和IMyAidlInterface 是同一類型 強轉後返回該對象 否則返回Proxy 代理對象
onTransact()負責對自己定義的接口方法的調用
@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_basicTypes: {
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
long _arg1;
_arg1 = data.readLong();
boolean _arg2;
_arg2 = (0 != data.readInt());
float _arg3;
_arg3 = data.readFloat();
double _arg4;
_arg4 = data.readDouble();
java.lang.String _arg5;
_arg5 = data.readString();
this.basicTypes(_arg0, _arg1, _arg2, _arg3, _arg4, _arg5);
reply.writeNoException();
return true;
}
case TRANSACTION_add: {
data.enforceInterface(DESCRIPTOR);
this.add();
reply.writeNoException();
return true;
}
case TRANSACTION_delete: {
data.enforceInterface(DESCRIPTOR);
this.delete();
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
調用時通過code來匹配 下面的 int值
static final int TRANSACTION_basicTypes = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
static final int TRANSACTION_delete = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);