版权声明:本文出自汪磊的博客,转载请务必注明出处。

一、概述

Android开发的时候,我们时长遇到传递对象的需求,但是我们无法将对象的引用传给Activity或者Fragment,我们需要将这些对象放到一个Intent或者Bundle里面,然后再传递,这时候就用到了序列化,所谓序列化就是把Java对象转换为字节序列并存储至一个储存媒介的过程,反序列化就是把字节序列恢复为Java对象的过程。但是我们要知道序列化与反序列化仅处理Java变量而不处理方法,仅对数据进行处理。

序列化两种方式

Android中序列化有两种方式:Serializable以及Parcelable。其中Serializable是Java自带的,而Parcelable是安卓专有的。关于二者区别我们最后会总结,先看看怎么使用吧。

Serializable方式序列化实例

serializable使用比较简单,只需要对某个类实现Serializable 接口即可。

Serializable 接口是一种标识接口,某个类实现Serializable 接口,Java便会对这个对象进行序列化操作。

我们编写Person类:

  1. 1 public class Person implements Serializable {
  2. 2
  3. 3 private static final long serialVersionUID = -3139325922167935911L;
  4. 4 //
  5. 5 private int age;
  6. 6 private String name;
  7. 7
  8. 8 public int getAge() {
  9. 9 return age;
  10. 10 }
  11. 11
  12. 12 public void setAge(int age) {
  13. 13 this.age = age;
  14. 14 }
  15. 15
  16. 16 public String getName() {
  17. 17 return name;
  18. 18 }
  19. 19
  20. 20 public void setName(String name) {
  21. 21 this.name = name;
  22. 22 }
  23. 23
  24. 24 }

很简单吧,无需过多解释。接下来我们就将这个类从一个Acticity传递到另一个Activity。

MainActivity:

  1. 1 public class MainActivity extends Activity {
  2. 2
  3. 3 //
  4. 4 @Override
  5. 5 protected void onCreate(Bundle savedInstanceState) {
  6. 6 super.onCreate(savedInstanceState);
  7. 7 setContentView(R.layout.activity_main);
  8. 8 //
  9. 9 Person p = new Person();
  10. 10 p.setAge(18);
  11. 11 p.setName("wanglei");
  12. 12 //
  13. 13 Intent i = new Intent(this, SecondActivity.class);
  14. 14 i.putExtra("person", p);
  15. 15 startActivity(i);
  16. 16
  17. 17 }
  18. 18 }

 SecondActivity:

  1. 1 public class SecondActivity extends Activity {
  2. 2
  3. 3 //
  4. 4
  5. 5 private static final String TAG = "WL";
  6. 6
  7. 7 @Override
  8. 8 protected void onCreate(Bundle savedInstanceState) {
  9. 9 super.onCreate(savedInstanceState);
  10. 10 setContentView(R.layout.activity_main);
  11. 11
  12. 12 Intent intent = getIntent();
  13. 13 Person p=(Person) intent.getSerializableExtra("person");
  14. 14
  15. 15 Log.i(TAG, "age = "+p.getAge());
  16. 16 Log.i(TAG, "name = "+p.getName());
  17. 17 }
  18. 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. 1 public class Person implements Parcelable {
  2. 2
  3. 3 //
  4. 4 private int age;
  5. 5 private String name;
  6. 6 private int weight;
  7. 7
  8. 8 Person(){
  9. 9
  10. 10 }
  11. 11
  12. 12 Person(Parcel in){
  13. 13 age = in.readInt();
  14. 14 name = in.readString();
  15. 15 weight = in.readInt();
  16. 16 }
  17. 17
  18. 18
  19. 19 //序列化时指定将哪些数据写入Parcel中,注意:写入顺序与读取顺序务必一致
  20. 20 @Override
  21. 21 public void writeToParcel(Parcel dest, int flags) {
  22. 22 //
  23. 23 dest.writeInt(age);
  24. 24 dest.writeString(name);
  25. 25 dest.writeInt(weight);
  26. 26 }
  27. 27
  28. 28 //这里一定要写上public关键字,我测试如果不写会报异常,此外名字不能改必须为:CREATOR
  29. 29 public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
  30. 30
  31. 31 @Override
  32. 32 public Person[] newArray(int size) {
  33. 33 //
  34. 34 return new Person[size];
  35. 35 }
  36. 36
  37. 37 //反序列化时从Parcel中读取数据
  38. 38 @Override
  39. 39 public Person createFromParcel(Parcel source) {
  40. 40 //
  41. 41 return new Person(source);
  42. 42 }
  43. 43 };
  44. 44
  45. 45 @Override
  46. 46 public int describeContents() {
  47. 47 //默认返回0即可
  48. 48 return 0;
  49. 49 }
  50. 50
  51. 51 //
  52. 52 public int getAge() {
  53. 53 return age;
  54. 54 }
  55. 55
  56. 56 public void setAge(int age) {
  57. 57 this.age = age;
  58. 58 }
  59. 59
  60. 60 public String getName() {
  61. 61 return name;
  62. 62 }
  63. 63
  64. 64 public void setName(String name) {
  65. 65 this.name = name;
  66. 66 }
  67. 67
  68. 68 public int getWeight() {
  69. 69 return weight;
  70. 70 }
  71. 71
  72. 72 public void setWeight(int weight) {
  73. 73 this.weight = weight;
  74. 74 }
  75. 75
  76. 76 }

很多注意点都在注释中写出来了,仔细看注释即可。

Persons类:盛放Person类,主要演示如何序列化与反序列化List集合数据,这里似乎有点麻烦了,不过已经这样写了就这样举例吧。

  1. 1 public class Persons implements Parcelable {
  2. 2
  3. 3 private List<Person> mList;
  4. 4
  5. 5 Persons() {
  6. 6
  7. 7 }
  8. 8
  9. 9 Persons(Parcel in) {
  10. 10
  11. 11 this.mList = new ArrayList<Person>();
  12. 12 in.readTypedList(mList, Person.CREATOR);
  13. 13 }
  14. 14
  15. 15 @Override
  16. 16 public int describeContents() {
  17. 17 //
  18. 18 return 0;
  19. 19 }
  20. 20
  21. 21 @Override
  22. 22 public void writeToParcel(Parcel dest, int flags) {
  23. 23 //
  24. 24 dest.writeTypedList(mList);
  25. 25 }
  26. 26
  27. 27 public static final Parcelable.Creator<Persons> CREATOR = new Creator<Persons>() {
  28. 28
  29. 29 @Override
  30. 30 public Persons[] newArray(int size) {
  31. 31 //
  32. 32 return new Persons[size];
  33. 33 }
  34. 34
  35. 35 @Override
  36. 36 public Persons createFromParcel(Parcel source) {
  37. 37 //
  38. 38 return new Persons(source);
  39. 39 }
  40. 40 };
  41. 41
  42. 42
  43. 43 public List<Person> getmList() {
  44. 44 return mList;
  45. 45 }
  46. 46
  47. 47 public void setmList(List<Person> mList) {
  48. 48 this.mList = mList;
  49. 49 }
  50. 50
  51. 51 }

Persons主要演示如何反序列化集合类数据,如11,12行代码。

MainActivity:

  1. 1 public class MainActivity extends Activity {
  2. 2
  3. 3 //
  4. 4 @Override
  5. 5 protected void onCreate(Bundle savedInstanceState) {
  6. 6 super.onCreate(savedInstanceState);
  7. 7 setContentView(R.layout.activity_main);
  8. 8 //
  9. 9 Person p1 = new Person();
  10. 10 p1.setAge(18);
  11. 11 p1.setName("wanglei1");
  12. 12 p1.setWeight(130);
  13. 13 //
  14. 14 Person p2 = new Person();
  15. 15 p2.setAge(28);
  16. 16 p2.setName("wanglei2");
  17. 17 p2.setWeight(125);
  18. 18 //
  19. 19 List<Person> mList = new ArrayList<Person>();
  20. 20 mList.add(p1);
  21. 21 mList.add(p2);
  22. 22 //
  23. 23 Persons mPersons = new Persons();
  24. 24 mPersons.setmList(mList);
  25. 25 //
  26. 26 Intent i = new Intent(this, SecondActivity.class);
  27. 27 i.putExtra("persons", mPersons);
  28. 28 startActivity(i);
  29. 29
  30. 30 }
  31. 31 }

也很简单吧,没什么要特别说明的。

SecondActivity:

  1. 1 public class SecondActivity extends Activity {
  2. 2
  3. 3 //
  4. 4
  5. 5 private static final String TAG = "WL";
  6. 6
  7. 7 @Override
  8. 8 protected void onCreate(Bundle savedInstanceState) {
  9. 9 super.onCreate(savedInstanceState);
  10. 10 setContentView(R.layout.activity_main);
  11. 11
  12. 12 Intent intent = getIntent();
  13. 13 Persons ps = (Persons) intent.getParcelableExtra("persons");
  14. 14 List<Person> pList = ps.getmList();
  15. 15 for (int i = 0; i < pList.size(); i++) {
  16. 16 Person p = pList.get(i);
  17. 17 Log.i(TAG, "age = " + p.getAge());
  18. 18 Log.i(TAG, "name = " + p.getName());
  19. 19 Log.i(TAG, "weight = " + p.getWeight());
  20. 20 }
  21. 21
  22. 22 }
  23. 23 }

主要就是通过intent.getParcelableExtra获取序列化的对象,也很简单。

运行程序结果如下:

数据传递成功,如果仔细看上面例子应该对Parcelable方式实现序列化有了一定的了解,貌似写起来会比Serializable方式复杂一些,很多都是模板代码,照着写就是了,那这两种方式有什么区别呢?

、两种序列化方式区别

两者区别在于存储媒介的不同。

Serializable使用IO读写存储在硬盘上。序列化过程使用了反射技术,并且期间产生临时对象。优点代码少。

Parcelable是直接在内存中读写,我们知道内存的读写速度肯定优于硬盘读写速度,所以Parcelable序列化方式性能上要优于Serializable方式很多。但是代码写起来相比Serializable方式麻烦一些。

通过比较发现,性能与简便我们只能选其一,大多数情况下使用Serializable也是没什么问题的,但是还是建议大家使用Parcelable方式实现序列化,毕竟性能好很多,其实也没多麻烦。

好了,本文到此结束,希望对你有一些帮助。

版权声明:本文为leipDao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:http://www.cnblogs.com/leipDao/p/8022063.html