概述
- 第一:实现Parcelable接口
- 第二:定义一个Parcelable.Creator类型的CREATOR对象
- 第三:要提供一个Booka.aidl文件,其中内容为parcelable Booka,定义了之后,在其他aidl文件中引用Booka时便不会提示出错了。
- Booka.java
- public class Booka implements Parcelable {
private String bookName;
private int bookPrice;
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getBookPrice() {
return bookPrice;
}
public void setBookPrice(int bookPrice) {
this.bookPrice = bookPrice;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(bookName);
parcel.writeInt(bookPrice);
}
public static final Parcelable.Creator<Booka> CREATOR = new Creator<Booka>() {
public Booka createFromParcel(Parcel source) {
Booka a=new Booka();
a.setBookName(source.readString());
a.setBookPrice(source.readInt());
return a;
}
public Booka[] newArray(int size) {
return new Booka[size];
}
};
}
- import 导入相应的包;
- Booka.aidl parcelable Booka;
- Ibooka
- interface Ibooka{
Booka getBook();
}
- 服务端代码
- public class RemoteService extends Service {
private final static String TAG = "RemoteService";
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "执行了OnBind");
return new MyBinder();
}
private class MyBinder extends aaaa.Stub {
@Override
public Booka getBook() throws RemoteException {
Booka a = new Booka();
a.setBookName("金瓶梅");
a.setBookPrice(125);
return a;
}
}
}
- 客户端代码
- private class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
aaaa asInterface = aaaa.Stub.asInterface(service);
if (asInterface == null) {
return;
}
try { - //获取另一个程序的Booka对象
Booka allInfo = asInterface.getBook(); -
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
MyServiceConnection connection = new MyServiceConnection();
- Intent intent = new Intent(“隐士意图的action”);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
-
最后
以上就是无语香氛为你收集整理的aidl 传递对象的全部内容,希望文章能够帮你解决aidl 传递对象所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复