浅谈Android中Serializable和Parcelable使用区别
版权声明:本文出自汪磊的博客,转载请务必注明出处。
一、概述
Android开发的时候,我们时长遇到传递对象的需求,但是我们无法将对象的引用传给Activity或者Fragment,我们需要将这些对象放到一个Intent或者Bundle里面,然后再传递,这时候就用到了序列化,所谓序列化就是把Java对象转换为字节序列并存储至一个储存媒介的过程,反序列化就是把字节序列恢复为Java对象的过程。但是我们要知道序列化与反序列化仅处理Java变量而不处理方法,仅对数据进行处理。
二、序列化两种方式
Android中序列化有两种方式:Serializable以及Parcelable。其中Serializable是Java自带的,而Parcelable是安卓专有的。关于二者区别我们最后会总结,先看看怎么使用吧。
三、Serializable方式序列化实例
serializable使用比较简单,只需要对某个类实现Serializable 接口即可。
Serializable 接口是一种标识接口,某个类实现Serializable 接口,Java便会对这个对象进行序列化操作。
我们编写Person类:
- 1 public class Person implements Serializable {
- 2
- 3 private static final long serialVersionUID = -3139325922167935911L;
- 4 //
- 5 private int age;
- 6 private String name;
- 7
- 8 public int getAge() {
- 9 return age;
- 10 }
- 11
- 12 public void setAge(int age) {
- 13 this.age = age;
- 14 }
- 15
- 16 public String getName() {
- 17 return name;
- 18 }
- 19
- 20 public void setName(String name) {
- 21 this.name = name;
- 22 }
- 23
- 24 }
很简单吧,无需过多解释。接下来我们就将这个类从一个Acticity传递到另一个Activity。
MainActivity:
- 1 public class MainActivity extends Activity {
- 2
- 3 //
- 4 @Override
- 5 protected void onCreate(Bundle savedInstanceState) {
- 6 super.onCreate(savedInstanceState);
- 7 setContentView(R.layout.activity_main);
- 8 //
- 9 Person p = new Person();
- 10 p.setAge(18);
- 11 p.setName("wanglei");
- 12 //
- 13 Intent i = new Intent(this, SecondActivity.class);
- 14 i.putExtra("person", p);
- 15 startActivity(i);
- 16
- 17 }
- 18 }
SecondActivity:
- 1 public class SecondActivity extends Activity {
- 2
- 3 //
- 4
- 5 private static final String TAG = "WL";
- 6
- 7 @Override
- 8 protected void onCreate(Bundle savedInstanceState) {
- 9 super.onCreate(savedInstanceState);
- 10 setContentView(R.layout.activity_main);
- 11
- 12 Intent intent = getIntent();
- 13 Person p=(Person) intent.getSerializableExtra("person");
- 14
- 15 Log.i(TAG, "age = "+p.getAge());
- 16 Log.i(TAG, "name = "+p.getName());
- 17 }
- 18 }
以上代码及其简单了就不解释了,运行程序会看到如下打印:
以上就是Serializable方式序列化对象的举例,真的很简单,没有什么多余要解释的。
四、Parcelable方式序列化实例
关与Parcelable方式实现序列化会比Serializable 方法麻烦一些,大体步骤如下:
1. 实现Parcelable接口
2. 覆写describeContents方法,默认返回0。
3. 覆写writeToParcel(Parcel dest, int flags)方法,指定写入Parcel类的数据。
4. 创建Parcelable.Creator静态对象,覆写方法createFromParcel(Parcel in)与newArray(int size)。
Person类:
- 1 public class Person implements Parcelable {
- 2
- 3 //
- 4 private int age;
- 5 private String name;
- 6 private int weight;
- 7
- 8 Person(){
- 9
- 10 }
- 11
- 12 Person(Parcel in){
- 13 age = in.readInt();
- 14 name = in.readString();
- 15 weight = in.readInt();
- 16 }
- 17
- 18
- 19 //序列化时指定将哪些数据写入Parcel中,注意:写入顺序与读取顺序务必一致
- 20 @Override
- 21 public void writeToParcel(Parcel dest, int flags) {
- 22 //
- 23 dest.writeInt(age);
- 24 dest.writeString(name);
- 25 dest.writeInt(weight);
- 26 }
- 27
- 28 //这里一定要写上public关键字,我测试如果不写会报异常,此外名字不能改必须为:CREATOR
- 29 public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
- 30
- 31 @Override
- 32 public Person[] newArray(int size) {
- 33 //
- 34 return new Person[size];
- 35 }
- 36
- 37 //反序列化时从Parcel中读取数据
- 38 @Override
- 39 public Person createFromParcel(Parcel source) {
- 40 //
- 41 return new Person(source);
- 42 }
- 43 };
- 44
- 45 @Override
- 46 public int describeContents() {
- 47 //默认返回0即可
- 48 return 0;
- 49 }
- 50
- 51 //
- 52 public int getAge() {
- 53 return age;
- 54 }
- 55
- 56 public void setAge(int age) {
- 57 this.age = age;
- 58 }
- 59
- 60 public String getName() {
- 61 return name;
- 62 }
- 63
- 64 public void setName(String name) {
- 65 this.name = name;
- 66 }
- 67
- 68 public int getWeight() {
- 69 return weight;
- 70 }
- 71
- 72 public void setWeight(int weight) {
- 73 this.weight = weight;
- 74 }
- 75
- 76 }
很多注意点都在注释中写出来了,仔细看注释即可。
Persons类:盛放Person类,主要演示如何序列化与反序列化List集合数据,这里似乎有点麻烦了,不过已经这样写了就这样举例吧。
- 1 public class Persons implements Parcelable {
- 2
- 3 private List<Person> mList;
- 4
- 5 Persons() {
- 6
- 7 }
- 8
- 9 Persons(Parcel in) {
- 10
- 11 this.mList = new ArrayList<Person>();
- 12 in.readTypedList(mList, Person.CREATOR);
- 13 }
- 14
- 15 @Override
- 16 public int describeContents() {
- 17 //
- 18 return 0;
- 19 }
- 20
- 21 @Override
- 22 public void writeToParcel(Parcel dest, int flags) {
- 23 //
- 24 dest.writeTypedList(mList);
- 25 }
- 26
- 27 public static final Parcelable.Creator<Persons> CREATOR = new Creator<Persons>() {
- 28
- 29 @Override
- 30 public Persons[] newArray(int size) {
- 31 //
- 32 return new Persons[size];
- 33 }
- 34
- 35 @Override
- 36 public Persons createFromParcel(Parcel source) {
- 37 //
- 38 return new Persons(source);
- 39 }
- 40 };
- 41
- 42
- 43 public List<Person> getmList() {
- 44 return mList;
- 45 }
- 46
- 47 public void setmList(List<Person> mList) {
- 48 this.mList = mList;
- 49 }
- 50
- 51 }
Persons主要演示如何反序列化集合类数据,如11,12行代码。
MainActivity:
- 1 public class MainActivity extends Activity {
- 2
- 3 //
- 4 @Override
- 5 protected void onCreate(Bundle savedInstanceState) {
- 6 super.onCreate(savedInstanceState);
- 7 setContentView(R.layout.activity_main);
- 8 //
- 9 Person p1 = new Person();
- 10 p1.setAge(18);
- 11 p1.setName("wanglei1");
- 12 p1.setWeight(130);
- 13 //
- 14 Person p2 = new Person();
- 15 p2.setAge(28);
- 16 p2.setName("wanglei2");
- 17 p2.setWeight(125);
- 18 //
- 19 List<Person> mList = new ArrayList<Person>();
- 20 mList.add(p1);
- 21 mList.add(p2);
- 22 //
- 23 Persons mPersons = new Persons();
- 24 mPersons.setmList(mList);
- 25 //
- 26 Intent i = new Intent(this, SecondActivity.class);
- 27 i.putExtra("persons", mPersons);
- 28 startActivity(i);
- 29
- 30 }
- 31 }
也很简单吧,没什么要特别说明的。
SecondActivity:
- 1 public class SecondActivity extends Activity {
- 2
- 3 //
- 4
- 5 private static final String TAG = "WL";
- 6
- 7 @Override
- 8 protected void onCreate(Bundle savedInstanceState) {
- 9 super.onCreate(savedInstanceState);
- 10 setContentView(R.layout.activity_main);
- 11
- 12 Intent intent = getIntent();
- 13 Persons ps = (Persons) intent.getParcelableExtra("persons");
- 14 List<Person> pList = ps.getmList();
- 15 for (int i = 0; i < pList.size(); i++) {
- 16 Person p = pList.get(i);
- 17 Log.i(TAG, "age = " + p.getAge());
- 18 Log.i(TAG, "name = " + p.getName());
- 19 Log.i(TAG, "weight = " + p.getWeight());
- 20 }
- 21
- 22 }
- 23 }
主要就是通过intent.getParcelableExtra获取序列化的对象,也很简单。
运行程序结果如下:
数据传递成功,如果仔细看上面例子应该对Parcelable方式实现序列化有了一定的了解,貌似写起来会比Serializable方式复杂一些,很多都是模板代码,照着写就是了,那这两种方式有什么区别呢?
五、两种序列化方式区别
两者区别在于存储媒介的不同。
Serializable使用IO读写存储在硬盘上。序列化过程使用了反射技术,并且期间产生临时对象。优点代码少。
Parcelable是直接在内存中读写,我们知道内存的读写速度肯定优于硬盘读写速度,所以Parcelable序列化方式性能上要优于Serializable方式很多。但是代码写起来相比Serializable方式麻烦一些。
通过比较发现,性能与简便我们只能选其一,大多数情况下使用Serializable也是没什么问题的,但是还是建议大家使用Parcelable方式实现序列化,毕竟性能好很多,其实也没多麻烦。
好了,本文到此结束,希望对你有一些帮助。