最近在使用AIDL作IPC的时候,在处理复杂的数据类型的时候,编译器老是报couldn't find import for class错误,因此在这里总结下AIDL使用的时候的一些注意事项,但愿对你能有所帮助。html
Android 中进程间通讯使用了 AIDL 语言,可是支持的数据类型有限:android
1.Java的简单类型(int、char、boolean等)。不须要导入(import)。ide
2.String和CharSequence。不须要导入(import)。ui
3.List和Map。但要注意,List和Map对象的元素类型必须是AIDL服务支持的数据类型。不须要导入(import)。spa
4.AIDL自动生成的接口。须要导入(import)。code
5.实现android.os.Parcelable接口的类。须要导入(import)。component
其中后两种数据类型须要使用import进行导入。htm
刚开始想固然的觉得传递Object只要实现了Parcelable接口在AIDL文件中导入便可, 而后编译器立刻战胜了个人天真,couldn't find import for class!!!,好吧仍是老老实实的去看文档吧。AIDL文档连接对象
解决这个错误的步骤以下:blog
1.确保你的类已经实现了 Parcelable接口
2.实现writeToParcel方法,将对象的当前状态写入
Parcel
3.添加一个叫 CREATOR
的静态字段,它实现了 Parcelable.Creator
4.最后建立一个对应的*.aidl文件去声明你的Parcelable类
例如
package android.graphics; // Declare Rect so AIDL can find it and knows that it implements // the parcelable protocol. parcelable Rect;
import android.os.Parcel; import android.os.Parcelable; public final class Rect implements Parcelable { public int left; public int top; public int right; public int bottom; public static final Parcelable.Creator<Rect> CREATOR = new Parcelable.Creator<Rect>() { public Rect createFromParcel(Parcel in) { return new Rect(in); } public Rect[] newArray(int size) { return new Rect[size]; } }; public Rect() { } private Rect(Parcel in) { readFromParcel(in); } public void writeToParcel(Parcel out) { out.writeInt(left); out.writeInt(top); out.writeInt(right); out.writeInt(bottom); } public void readFromParcel(Parcel in) { left = in.readInt(); top = in.readInt(); right = in.readInt(); bottom = in.readInt(); } }