Parcalable接口使用(android传递结构体数据的方法)

对于Android来说传递复杂类型,主要是将自己的类转换为基础的字节数组,Activity之间传递数据是通过Intent实现的。 Android序列化对象主要有两种方法,实现Serializable接口、或者实现Parcelable接口。实现Serializable接口是Java SE本身就支持的,而Parcelable是Android特有的功能,效率比实现Serializable接口高,而且还可以用在进程间通信(IPC)中。实现Serializable接口非常简单,声明一下就可以了。而实现Parcelable接口稍微复杂一些,但效率更高,推荐用这种方法提高性能。

android 中 自定义的对象序列化的问题 有两个选择一个是Parcelable,另外一个是Serializable。

一 序列化原因:

1.永久性保存对象,保存对象的字节序列到本地文件中;
2.通过序列化对象在网络中传递对象;
3.通过序列化在进程间传递对象。 

二 至于选取哪种可参考下面的原则:

1.在使用内存的时候,Parcelable 类比Serializable性能高,所以推荐使用Parcelable类。
2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点, 也不提倡用,但在这种情况下,还是建议你用Serializable 。


Parcelable接口的作用:实现了Parcelable接口的实例可以将自身的状态信息(状态信息通常指的是各成员变量的值)写入Parcel,也可以从Parcel中恢复其状态。 Parcel用来完成数据的序列化传递。下面就介绍一下实现Parcelable接口的方法。

 通过实现Parcelable接口序列化对象的步骤: 

1、实现Parcelable接口。

2、并且实现Parcelable接口的public void writeToParcel(Parcel dest, int flags)方法 。

3、自定义类型中必须含有一个名称为CREATOR的静态成员,该成员对象要求实现Parcelable.Creator接口及其方法。

简而言之:通过writeToParcel将你的对象映射成Parcel对象,再通过createFromParcel将Parcel对象映射成你的对象。也可以将Parcel看成是一个流,通过writeToParcel把对象写到流里面,在通过createFromParcel从流里读取对象,只不过这个过程需要你来实现,因此写的顺序和读的顺序必须一致。

示例代码:

[java]  view plain copy
  1. package com.yang.domain;  
  2.   
  3. import android.os.Parcel;  
  4. import android.os.Parcelable;  
  5.   
  6. public class Person implements Parcelable {  
  7.      //这里定义了两个变量来说明读和写的顺序要一致    
  8.     private Integer id;  
  9.     private String name;  
  10.   
  11.     public Person() {  
  12.     }  
  13.   
  14.     public Person(Integer id, String name) {  
  15.           
  16.         this.id = id;  
  17.         this.name = name;  
  18.     }  
  19.   
  20.     public Integer getId() {  
  21.         return id;  
  22.     }  
  23.   
  24.     public void setId(Integer id) {  
  25.         this.id = id;  
  26.     }  
  27.   
  28.     public String getName() {  
  29.         return name;  
  30.     }  
  31.   
  32.     public void setName(String name) {  
  33.         this.name = name;  
  34.     }  
  35.   
  36.     @Override  
  37.     public int describeContents() {  
  38.         return 0;  
  39.     }  
  40.   
  41.     @Override  
  42.     public void writeToParcel(Parcel dest, int flags) {  
  43.         // 把javanbean中的数据写到Parcel。先写id然后写name  
  44.         dest.writeInt(this.id);  
  45.         dest.writeString(this.name);  
  46.     }  
  47.   
  48.     // 添加一个静态成员,名为CREATOR,该对象实现了Parcelable.Creator接口  
  49.     public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {  
  50.         @Override  
  51.         public Person createFromParcel(Parcel source) {  
  52.             // 从Parcel中读取数据,返回person对象  
  53.             return new Person(source.readInt(), source.readString());  
  54.         }  
  55.   
  56.         @Override  
  57.         public Person[] newArray(int size) {  
  58.             return new Person[size];  
  59.         }  
  60.     };  
  61. }  
要传递的数据是由复制数据类型组合而成时:
[java]  view plain copy
  1. public class MyParcelable implements Parcelable {  
  2.   
  3.     private List<MyListClass> arrList = new ArrayList<MyListClass>();  
  4.     private int myInt = 0;  
  5.     private String str = null;  
  6.   
  7.     public String getStr() {  
  8.         return str;  
  9.     }  
  10.   
  11.     public void setStr(String str) {  
  12.         this.str = str;  
  13.     }  
  14.   
  15.     public List<MyListClass> getArrList() {  
  16.         return arrList;  
  17.     }  
  18.   
  19.     public void setArrList(List<MyListClass> arrList) {  
  20.         this.arrList = arrList;  
  21.     }  
  22.   
  23.     public int getMyInt() {  
  24.         return myInt;  
  25.     }  
  26.   
  27.     public void setMyInt(int myInt) {  
  28.         this.myInt = myInt;  
  29.     }  
  30.   
  31.     MyParcelable() {  
  32.         // initialization  
  33.         arrList = new ArrayList<MyListClass>();  
  34.     }  
  35.   
  36.     public MyParcelable(Parcel in) {  
  37.         myInt = in.readInt();  
  38.         str = in.readString();  
  39.         in.readTypedList(arrList, MyListClass.CREATOR);  
  40.     }  
  41.   
  42.     @Override  
  43.     public int describeContents() {  
  44.         return 0;  
  45.     }  
  46.   
  47.     @Override  
  48.     public void writeToParcel(Parcel outParcel, int flags) {  
  49.         outParcel.writeInt(myInt);  
  50.         outParcel.writeString(str);  
  51.         outParcel.writeTypedList(arrList);  
  52.     }  
  53.   
  54.     public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {  
  55.   
  56.         @Override  
  57.         public MyParcelable createFromParcel(Parcel in) {  
  58.             return new MyParcelable(in);  
  59.         }  
  60.   
  61.         @Override  
  62.         public MyParcelable[] newArray(int size) {  
  63.             return new MyParcelable[size];  
  64.         }  
  65.     };  
  66. }  
当有子类父类情况时
[java]  view plain copy
  1. public abstract class A implements Parcelable {  
  2.     private int a;  
  3.   
  4.     protected A(int a) {  
  5.         this.a = a;  
  6.     }  
  7.   
  8.     public void writeToParcel(Parcel out, int flags) {  
  9.         out.writeInt(a);  
  10.     }  
  11.   
  12.     protected A(Parcel in) {  
  13.         a = in.readInt();  
  14.     }  
  15. }  
  16.   
  17. public class B extends A {  
  18.     private int b;  
  19.   
  20.     public B(int a, int b) {  
  21.         super(a);  
  22.         this.b = b;  
  23.     }  
  24.   
  25.     public static final Parcelable.Creator<B> CREATOR = new Parcelable.Creator<B>() {  
  26.         public B createFromParcel(Parcel in) {  
  27.             return new B(in);  
  28.         }  
  29.   
  30.         public B[] newArray(int size) {  
  31.             return new B[size];  
  32.         }  
  33.     };  
  34.   
  35.     public int describeContents() {  
  36.         return 0;  
  37.     }  
  38.   
  39.     public void writeToParcel(Parcel out, int flags) {  
  40.         super.writeToParcel(out, flags);  
  41.         out.writeInt(b);  
  42.     }  
  43.   
  44.     private B(Parcel in) {  
  45.         super(in);  
  46.         b = in.readInt();  
  47.     }  
  48. }  

注:Parcelable接口在Android当中有非常之多的子类,如下截图:

并且Intent当中也定义了很多关于Parcelable的get、set方法,如:
Intent putExtra( String name,  Parcelable value)
Add extended data to the intent.
<T extends  Parcelable> T getParcelableExtra( String name)
Retrieve extended data from the intent.
并且Intent本身也实现了 Parcelable接口,因此在Android开发当中是非常推荐以Parcelable作为工具传递复制对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值