首页 > 代码库 > Android开发 AIDL使用自定义对象作参数或返回值

Android开发 AIDL使用自定义对象作参数或返回值

http://www.pocketdigi.com/20121129/952.html

默认,AIDL支持对象作参数,但需要该对象实现Parcelable接口,且aidl文件应该是该类在同一包下,需要单独给该类定义一个aidl文件.
定义模型类:
EnglishItem.java:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
package com.pocketdigi.english.aidl; import android.os.Parcel;import android.os.Parcelable; import com.j256.ormlite.field.DatabaseField;import com.j256.ormlite.table.DatabaseTable;import com.pocketdigi.english.utils.Constants;import com.pocketdigi.english.utils.Des; @DatabaseTable(tableName = "listenlist")public class EnglishItem implements Parcelable {	/**	 * 	 */	private static final long serialVersionUID = -1756522544697525757L;	@DatabaseField(id = true)	private int id;	@DatabaseField	private String title;	@DatabaseField	private String category;	@DatabaseField	private String mp3url;	@DatabaseField	private String lrcurl;	@DatabaseField	private String txturl; 	public static final Parcelable.Creator<EnglishItem> CREATOR = new Parcelable.Creator<EnglishItem>() {		public EnglishItem createFromParcel(Parcel in) {			return new EnglishItem(in);		} 		public EnglishItem[] newArray(int size) {			return new EnglishItem[size];		}	}; 	private EnglishItem(Parcel in)	{		readFromParcel(in);	} 	public int getId() {		return id;	} 	public void setId(int id) {		this.id = id;	} 	public String getTitle() {		return title;	} 	public void setTitle(String title) {		this.title = title;	} 	public String getCategory() {		return category;	} 	public void setCategory(String category) {		this.category = category;	} 	public String getMp3url() {		return Des.decodeValue(Constants.DES_KEY, mp3url);	} 	public void setMp3url(String mp3url) {		this.mp3url = mp3url;	} 	public String getLrcurl() {		return Des.decodeValue(Constants.DES_KEY, lrcurl);	} 	public void setLrcurl(String lrcurl) {		this.lrcurl = lrcurl;	} 	public String getTxturl() {		return Des.decodeValue(Constants.DES_KEY, txturl);	} 	public void setTxturl(String txturl) {		this.txturl = txturl;	} 	@Override	public int describeContents() {		// TODO Auto-generated method stub		return 0;	} 	@Override	public void writeToParcel(Parcel out, int flag) {		// TODO Auto-generated method stub		out.writeInt(id);		out.writeString(title);		out.writeString(category);		out.writeString(mp3url);		out.writeString(lrcurl);		out.writeString(txturl);	}	/**	 * 读	 * @param in	 */	public void readFromParcel(Parcel in)	{		id=in.readInt();		title=in.readString();		category=in.readString();		category=in.readString();		lrcurl=in.readString();		txturl=in.readString();	}}

Parcelable.Creator不可少。
同包下定义一个EnglishItem.aidl:

12
package com.pocketdigi.english.aidl;parcelable EnglishItem;

最后是调用接口aidl:
PlayerAidl.aidl:

123456789101112131415161718192021222324252627
package com.pocketdigi.english.aidl; import com.pocketdigi.english.aidl.EnglishItem; interface PlayerAidl {	/**	*传入列表	**/	void putList(inout List<EnglishItem> item);	/**	*播放指定音频	**/	void play(int index);	void pause();	/**	*暂停后恢复播放	**/	void resume();	/**	*删除指定项	**/	void delete(int position);	/**	*停止服务	**/	void stopService();}