Android系统中Parcelable和Serializable的区别

来源:互联网 发布:威客网站 知乎 编辑:程序博客网 时间:2024/05/04 01:52

在进行Android开发的过程中,通常我们需要在不同的组件之间传递对象,我们可以在自定义的Application类中设置一个全局类变量(这是一个比较实用的方法),然后在不同的组件里去访问这个变量达到类变量传递的目的,这种方法对于那些访问比较频繁的对象实例,我觉得是可行的,但是我们操作的更多的是那些实用不频繁的类变量,如果也使用以上的方法,我觉得那就没有必要了,因为Application这个类会随着应用程序一直存在的,他的成员变量一直会保存下来,不经常使用的东西就不要保存在那里了。那我们又该使用什么方法呢?

想必大家知道我们可以在创建一个类的时候使用一个Serializable接口,

package com.meyhuan.weibo;import java.io.Serializable;public class Person implements Serializable{/** *  序列化得标志,默认为1L */private static final long serialVersionUID = 1L;private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}


然后可以通过类似这样传递对象了

// serializable对象传递方法public void setSerializableMethod() {  Person person = new Person();  person.setName("小明");  person.setAge(20);  Intent intent = new Intent(this, TestActivity.class);  Bundle bundle = new Bundle();  bundle.putSerializable(SER_KEY, person);  intent.putExtras(bundle);  startActivity(intent);} // 在TestActivity中就可以这样serializable对象public Book getSerializableMethod(){  Person person = (Person )getIntent().getSerializableExtra(SER_KEY);  return person;}


或者实现Parcelable接口,像这样:

package com.meyhuan.weibo;import android.os.Parcel;import android.os.Parcelable;public class Person implements Parcelable {  // 1、implements Parcelable接口。   private String mName;  private String mSex;  private int mAge;   public String getmName() {    return mName;  }   public void setmName(String mName) {    this.mName = mName;  }   public String getmSex() {    return mSex;  }   public void setmSex(String mSex) {    this.mSex = mSex;  }   public int getmAge() {    return mAge;  }   public void setmAge(int mAge) {    this.mAge = mAge;  }   // 2. 重写describeContents()方法,返回0即可  @Override  public int describeContents() {    return 0;  }   //3、重写writeToParcel方法,将你的对象序列化为一个Parcel对象  //即:将类的数据写入外部提供的Parcel中,打包需要传递的数据到Parcel容器保存,以便从Parcel容器获取数据。  @Override  public void writeToParcel(Parcel dest, int flags) {    dest.writeString(mName);    dest.writeString(mSex);    dest.writeInt(mAge);  }   //4、实例化静态内部对象CREATOR实现接口Parcelable.Creator   public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {     @Override    public Person createFromParcel(Parcel source) {      Person person = new Person();      person.mName = source.readString();      person.mSex = source.readString();      person.mAge = source.readInt();      return person;    }     //供反序列化本类数组时调用的    @Override    public Person[] newArray(int size) {      return new Person[size];    }  };}

通过这样来传递对象:


// <span style="font-family: Arial, Helvetica, sans-serif;">Parcelable</span>对象传递方法public void set<span style="font-family: Arial, Helvetica, sans-serif;">Parcelable</span><span style="font-family: Arial, Helvetica, sans-serif;">Method() {</span>  Person person = new Person();  person.setName("小明");  person.setAge(20);  Intent intent = new Intent(this, TestActivity.class);  Bundle bundle = new Bundle();  bundle.<span style="font-family: Arial, Helvetica, sans-serif;">putParcelable</span><span style="font-family: Arial, Helvetica, sans-serif;">(PAR_KEY, person);</span>  intent.putExtras(bundle);  startActivity(intent);} // 在TestActivity中就可以这样serializable对象public Book get<span style="font-family: Arial, Helvetica, sans-serif;">Parcelable</span><span style="font-family: Arial, Helvetica, sans-serif;">Method(){</span>  Person person = (Person )getIntent().getParcelableExtra(PAR_KEY);  return person;}
使用方法跟Serializable差不多

这两者除了用法上有一些不同,那么他的本质又有那些不一样呢?

1、Serializable 这种方法的缺点是使用了反射,序列化的过程较慢。这种机制会在序列化的时候创建许多的临时对象,容易触发垃圾回收。本质使用IO以文件的形式进行保存的。2

2、Parcelable, 是在android.os.Parcelable包下的,是Android特有的一种序列化方法,经测试后 Parcelable 比 Serializable快了10多倍。

但是它的实现可能会稍有点复杂,如果你是一个优秀的程序员那就使用Parceable吧


总结


如果你想成为一个优秀的软件工程师,你需要多花点时间来实现 Parcelable ,因为这将会为你对象的序列化过程快10多倍,而且占用较少的资源。


但是大多数情况下, Serializable 的龟速不会太引人注目。你想偷点懒就用它吧,不过要记得serialization是一个比较耗资源的操作,尽量少使用。


如果你想要传递一个包含许多对象的列表,那么整个序列化的过程的时间开销可能会超过一秒,这会让屏幕转向的时候变得很卡顿。





0 0
原创粉丝点击