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

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


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 CREATOR = new Parcelable.Creator() {
		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:


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

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


package com.pocketdigi.english.aidl;

import com.pocketdigi.english.aidl.EnglishItem;

interface PlayerAidl {
	/**
	*传入列表
	**/
	void putList(inout List item);
	/**
	*播放指定音频
	**/
	void play(int index);
	void pause();
	/**
	*暂停后恢复播放
	**/
	void resume();
	/**
	*删除指定项
	**/
	void delete(int position);
	/**
	*停止服务
	**/
	void stopService();
}

© 2012, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记