
安卓中实现序列化的接口有两个,一个是serializable,一个是parcelable。实现序列化一是可以将对象永久性保存在磁盘中,二是可以把对象在网络中传递,三是让对象在进程中传递。
Serializable与Parcelable的区别
1、Serializable是JDK提供的接口,而Parcelable是Android SDK提供的。
2、Serializable序列化是基于磁盘的,而Parcelable是基于内存的。在内存中读写肯定效率要高于磁盘,所以Android中跨进程传递对象都是使用Parcelable。
Parcelable接口定义
1 public interface Parcelable {
2 //内容描述接口,基本不用管
3 public int describeContents();
4 //写入接口函数,打包
5 public void writeToParcel(Parcel dest, int flags);
6 //读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。因为实现类在这里还是不可知的,所以需要用到模板的方式,继承类名通过模板参数传入。
7 //为了能够实现模板参数的传入,这里定义Creator嵌入接口,内含两个接口函数分别返回单个和多个继承类实例。
8 public interface Creator
{ 9 public T createFromParcel(Parcel
source); 10 public T[] newArray(int size); 11 }
从parcelable接口定义中,我们可以看到,实现parcelable接口,需要我们实现下面几个方法:
1.describeContents方法。内容接口描述,默认返回0就可以;
2.writeToParcel 方法。该方法将类的数据写入外部提供的Parcel中.即打包需要传递的数据到Parcel容器保存,以便从parcel容器获取数据,该方法声明如下:
writeToParcel (Parcel dest, int flags) 具体参数含义见javadoc
3.静态的Parcelable.Creator接口,本接口有两个方法:
createFromParcel(Parcel in) 从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层。
newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话(return new T[size])即可。方法是供外部类反序列化本类数组使用。
Parcelable的使用
1 public class AppContent implements Serializable, Parcelable {
2 //应用名字
3 private String name;
4 //应用下载链接
5 private String url;
6
7 private int downloadPercent = 0;
8
9 private Status status = Status.PENDING;
10
11 public AppContent(String name, String url) {
12 this.name = name;
13 this.url = url;
14 }
15
16 public String getName() {
17 return name;
18 }
19
20 public void setName(String name) {
21 this.name = name;
22 }
23
24 public String getUrl() {
25 return url;
26 }
27
28 public void setUrl(String url) {
29 this.url = url;
30 }
31
32 public int getDownloadPercent() {
33 return downloadPercent;
34 }
35
36 public void setDownloadPercent(int downloadPercent) {
37 this.downloadPercent = downloadPercent;
38 }
39
40 public Status getStatus() {
41 return status;
42 }
43
44 public void setStatus(Status status) {
45 this.status = status;
46 }
47
48 @Override
49 public String toString() {
50 return name;
51 }
52
53 @Override
54 public int describeContents() {
55 return 0;
56 }
57
58 //实现Parcel接口必须覆盖实现的方法
59 @Override
60 public void writeToParcel(Parcel dest, int flags) {
61 /*将AppContent的成员写入Parcel,
62 * 注:Parcel中的数据是按顺序写入和读取的,即先被写入的就会先被读取出来
63 */
64 dest.writeString(name);
65 dest.writeString(url);
66 dest.writeInt(downloadPercent);
67 dest.writeValue(status);
68 }
69
70 //该静态域是必须要有的,而且名字必须是CREATOR,否则会出错
71 public static final Parcelable.Creator
CREATOR = 72 new Parcelable.Creator
() { 73 74 @Override 75 public AppContent createFromParcel(Parcel
source) { 76 //从Parcel读取通过writeToParcel方法写入的AppContent的相关成员信息 77 String name = source.readString(); 78 String url = source.readString(); 79 int downloadPercent = source.readInt(); 80 Status status = (Status)source.readValue(new
ClassLoader(){}); 81 AppContent appContent = new AppContent(name, url); 82 appContent.setDownloadPercent(downloadPercent); 83 appContent.setStatus(status); 84 //更加读取到的信息,创建返回Person对象 85
return appContent; 86 } 87 88 @Override 89 public AppContent[] newArray(int size) 90 { 91 // TODO Auto-generated method stub 92 //返回AppContent对象数组 93
return new AppContent[size]; 94 } 95 }; 96 97 }
通过Intent进行传递:
1 Intent intent = new Intent(Constants.DOWNLOAD_MSG);
2 Bundle bundle = new Bundle();
3 bundle.putParcelable("appContent", appContent);
4 intent.putExtras(bundle);
文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/220052.html<