参考资料:谷歌官方文档
AIDL使用场景 :一对多通信且有RPC(Remote Procedure Call Protocol远程过程调用协议)需求
支持的类型:
- 基本数据类型:byte,int,long,char,booblean,double,float(除short)
- String和CharSequence
- List:只支持ArrayList,里面的元素都必须被AIDL支持
- Map:只支持HashMap,里面的元素都必须被AIDL支持
- Parcelable:所有实现了Parcelable接口的对象
- AIDL:所有AIDL接口本身也可以载AIDL中使用
Parcelable和AIDL必须要import,除基本类型外,其他类型的参数必须标上方向:in(输入)、out(输出)和inout(输入输出),建议把所有和AIDL相关的放在一个包中,便于复制到客户端使用。
服务端
// IRemoteService.aidl
package com.example.android;
// Declare any non-default types here with import statements
/** Example service interface */
interface IRemoteService {
/** Request the process ID of this service, to do evil things with it. */
int getPid();
/** 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);
}
android studio编译后生成IRemoteService.java(project/app/build/generated/source/aidl/debug/com/example/android/aidl/IRemoteService.java)
public class RemoteService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
// Return the interface
return mBinder;
}
private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public int getPid(){
return Process.myPid();
}
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString) {
// Does nothing
}
};
}
客户端
IRemoteService mIRemoteService;
private ServiceConnection mConnection = new ServiceConnection() {
// 建立连接时
public void onServiceConnected(ComponentName className, IBinder service) {
// Following the example above for an AIDL interface,
// this gets an instance of the IRemoteInterface, which we can use to call on the service
mIRemoteService = IRemoteService.Stub.asInterface(service);
}
// Called when the connection with the service disconnects unexpectedly
public void onServiceDisconnected(ComponentName className) {
Log.e(TAG, "Service has unexpectedly disconnected");
mIRemoteService = null;
}
};
//启动服务
private void startService() {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.android", "com.example.android.RemoteService"));
bindService(intent, mConnection, BIND_AUTO_CREATE);
}
继承Parcelable接口的对象
//创建一个Book类,实现Parcelable接口
public class Book implements Parcelable {
private int bookId;
private String bookName;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.bookId);
dest.writeString(this.bookName);
}
public Book() {
}
protected Book(Parcel in) {
this.bookId = in.readInt();
this.bookName = in.readString();
}
public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() {
@Override
public Book createFromParcel(Parcel source) {
return new Book(source);
}
@Override
public Book[] newArray(int size) {
return new Book[size];
}
};
@Override
public String toString() {
return "Book{" +
"bookId=" + bookId +
", bookName='" + bookName + '\'' +
'}';
}
}
创建与bean对象同名的aidl
//Book.aidl
package com.itzyf.aidl;
parcelable Book;
创建aidl服务端
package com.itzyf.aidl;
import com.itzyf.aidl.Book;
interface IBookManager {
List<Book> getBookList();
void addBook(in Book book);
}
其他文件同其他基本类型