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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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:

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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();
}

Android开发 AIDL使用自定义对象作参数或返回值,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。