在弄清AIDL是如何使用Binder實現(xiàn)進程間通信的之前,我們應該要知道我們在使用AIDL的簡要步驟
一、AIDL使用步驟
服務端:
創(chuàng)建對象Book.java,并實現(xiàn)Parcelable
新建AIDL文件(先隨意命名否則報錯),bookaidl.aidl,并在文件中聲明Book類
新建AIDL接口文件IBookManager.aidl,并且在其中定義方法(注意ADIL文件導包)
makeProject,系統(tǒng)自動于build文件夾下生成IBookManager.java
更改bookaidl.aidl為Book.aidl(要求必須與要傳遞對象同名,區(qū)分大小寫)
創(chuàng)建繼承Service的自定義服務AIDLService.java,并Manifest.xml中注冊(注意:必須添加intent-filter,意義在于使其他進程可以找到此服務)
聲明IBookManager.Stub類型變量(Binder類型),并實現(xiàn)接口方法(方法要使用同步方式)
重寫onBind方法返回步驟7中的binder
客戶端:
- 將服務端中AIDL文件夾移植到客戶端(注意:包名一致)
- 聲明IBookManager接口類型變量
- 聲明ServiceConnection類型變量,并完成bindService
- 在serviceConnected方法中通過IBookManager.Stub.asInterface(service)獲取IBookManager對象(應該要在子線程中調用)
- 這樣就可以使用IBookManager對象調用服務端接口中的定義的方法了
項目結構

image.png

image.png
#代碼一覽
###服務端
/**
* Created by Echo on 2019/5/14.
* Action
*/
public class Book implements Parcelable {
public int id;
public String name;
protected Book(Parcel in) {
id = in.readInt();
name = in.readString();
}
public static final Creator<Book> CREATOR = new Creator<Book>() {
@Override
public Book createFromParcel(Parcel in) {
return new Book(in);
}
@Override
public Book[] newArray(int size) {
return new Book[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
}
public String getName() {
return name;
}
}
// Book.aidl
package com.echo.mytts.aidl;
// Declare any non-default types here with import statements
parcelable Book;
// IBookManager.aidl
package com.echo.mytts.aidl;
// Declare any non-default types here with import statements
import com.echo.mytts.aidl.Book;
interface IBookManager {
/**
* 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);
List<Book> getBookList();
void addBook(in Book book);
}
/**
* Created by Echo on 2019/5/14.
* Action
*/
public class AIDLService extends Service {
private List<Book> mBookList = new ArrayList<>();
private IBookManager.Stub iBookManager = new IBookManager.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public List<Book> getBookList() throws RemoteException {
synchronized (mBookList){
Log.i("service","查看了圖書"+mBookList.size());
return mBookList;
}
}
@Override
public void addBook(Book book) throws RemoteException {
synchronized (mBookList){
mBookList.add(book);
Log.i("service","添加了圖書"+ book.getName());
}
}
};
@Override
public IBinder onBind(Intent intent) {
return iBookManager;
}
@Override
public void onCreate() {
super.onCreate();
Log.i("service","onCreate");
}
客戶端
public class MainActivity extends AppCompatActivity {
private IBookManager bookManager;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("client","連接service成功");
bookManager = IBookManager.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("client","取消連接");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.option).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bookManager == null)
return;
try {
Log.i("client","發(fā)起添加圖書請求");
bookManager.addBook(new Book(99,"AIDL"));
Log.i("client","發(fā)起查看圖書請求");
bookManager.getBookList();
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent();
intent.setAction("com.echo.mytts.aidl");
intent.setPackage("com.echo.mytts");
bindService(intent,serviceConnection,Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
unbindService(serviceConnection);
}