這個通訊是一個client ---service架構
我們先寫服務端的代碼,
一:我們先創建AIDL的接口,注意創建這個接口的路徑,到時候客戶端調用的時候要保證一致性。
interface ILeoAidl {
void addPerson(in Person person);
List<Person> getPersonList();
}
parcelable Person;
二:創建服務
<service
android:name=".LeoAidlService"
android:exported="true" />
public class LeoAidlService extends Service {
private ArrayList<Person> persons;
@Nullable
@Override
public IBinder onBind(Intent intent) {
persons = new ArrayList<>();
Log.e("LeoAidlService", "success onBind");
return iBinder;
}
private IBinder iBinder = new ILeoAidl.Stub() {
@Override
public void addPerson(Person person) throws RemoteException {
persons.add(person);
}
@Override
public List<Person> getPersonList() throws RemoteException {
return persons;
}
};
@Override
public void onCreate() {
super.onCreate();
Log.e("LeoAidlService", "onCreate: success");
}
}
三:創建實體類
public class Person implements Parcelable {
private String name;
private int grade;
public Person(String name, int grade) {
this.name = name;
this.grade = grade;
}
protected Person(Parcel in) {
this.name = in.readString();
this.grade = in.readInt();
}
public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(grade);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", grade=" + grade +
'}';
}
}
四:在界面中啓動服務
startService(new Intent(this, LeoAidlService.class));
五:創建客戶端項目,將剛剛的接口按照同樣的路徑複製到客戶端中
六:在客戶端也定義一個接受類,一模一樣
七:在界面中綁定服務
public class MainActivity extends AppCompatActivity {
private final static String TAG = "MainActivity";
private ILeoAidl iLeoAidl;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
bindService();
}
private void initView() {
btn = (Button) findViewById(R.id.but_click);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
iLeoAidl.addPerson(new Person("leo", 3));
List<Person> persons = iLeoAidl.getPersonList();
Log.e(TAG, persons.toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
private void bindService() {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.xx.leo_service", "com.xx.leo_service.LeoAidlService"));
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e(TAG, "onServiceConnected: success");
iLeoAidl = ILeoAidl.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e(TAG, "onServiceDisconnected: success");
iLeoAidl = null;
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
}
這樣就可以通訊了
有幾個常見的坑:
1:使用android studio,當我在某個aidl文件引用AIDL的時候,需要手動導包,一定要確認清楚。
2:注意parcelable聲明的AIDL所對應的的java實體類路徑名稱要一致
3:客戶端AIDL所在的路徑名稱要和服務端一致