【分析】Parcelable的作用

一、介绍

1.Parcelable是一个接口,可以实现序列化。

2.序列化的作用体现在:可以使用Intent来传递数据,也可以在进程建传递数据(IPC)。

3.Parcelable在使用的时候,有一个参数类型为:Parcel;它好比一个容器,序列化时,将数据传入。反序列化的时候,再将数据取出。

4.Parcelable不能再将要把数据存入磁盘的情况。因为,Parcelable在有外界的情况下不能有效的保证数据的持续性。

二、使用示例

1.被操作的数据实体和Parcelable的定义。

 1 package com.app.suodr.serialization;
 2 
 3 import android.os.Parcel;
 4 import android.os.Parcelable;
 5 
 6 /**
 7  * 作者:WangPei on 2015/7/16 09:43
 8  * 邮箱:460977141@qq.com
 9  *
10  * 通过writeToParcel将你的对象映射成Parcel对象,再通过createFromParcel将Parcel对象映射成你的对象。
11  * 也可以将Parcel看成是一个流,通过writeToParcel把对象写到流里面,在通过createFromParcel从流里读取对象,
12  * 只不过这个过程需要你来实现,因此写的顺序和读的顺序必须一致。
13  */
14 public class Parcelable_Person implements Parcelable{
15 
16     public String name;
17     public int age;
18 
19     public Parcelable_Person(String name, int age) {
20         this.name = name;
21         this.age = age;
22     }
23 
24     public String getName() {
25         return name;
26     }
27 
28     public void setName(String name) {
29         this.name = name;
30     }
31 
32     public int getAge() {
33         return age;
34     }
35 
36     public void setAge(int age) {
37         this.age = age;
38     }
39 
40     @Override
41     public int describeContents() {
42         return 0;
43     }
44 
45     @Override
46     public void writeToParcel(Parcel dest, int flags) {
47         dest.writeInt(age);
48         dest.writeString(name);
49     }
50 
51     public static final Creator<Parcelable_Person> CREATOR = new Creator<Parcelable_Person>() {
52         @Override
53         public Parcelable_Person createFromParcel(Parcel source) {
54             return new Parcelable_Person(source);
55         }
56 
57         @Override
58         public Parcelable_Person[] newArray(int size) {
59             return new Parcelable_Person[size];
60         }
61     };
62 
63     public Parcelable_Person(Parcel in) {
64         age = in.readInt();
65         name = in.readString();
66     }
67 }

2.使用Intent来传递数据分为:传递方和接收方

    传递方定义:

 1 package com.app.suodr.serialization;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 
 7 import com.app.suodr.ik_family.R;
 8 
 9 /**
10  * 作者:WangPei on 2015/7/16 10:06
11  * 邮箱:460977141@qq.com
12  */
13 public class Parcelable_Post extends Activity{
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.main);
19         Intent i = getIntent();
20 
21         Parcelable_Person person = new Parcelable_Person("wangpei",100);
22 
23         i.putExtra("yes",person);
24         i.setClass(this,Parcelable_Get.class);
25         startActivity(i);
26         this.finish();
27     }
28 }

    接收方定义:

 1 package com.app.suodr.serialization;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.util.Log;
 7 
 8 import com.app.suodr.ik_family.R;
 9 
10 /**
11  * 作者:WangPei on 2015/7/16 09:29
12  * 邮箱:460977141@qq.com
13  */
14 public class Parcelable_Get extends Activity{
15 
16     private static final String TAG = "myInfo";
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.main);
22 
23         Intent i = getIntent();
24         Parcelable_Person person = i.getParcelableExtra("yes");
25 
26         Log.i(TAG,"name:  "+person.name+"age:  "+person.age);
27     }
28 }

 

转载于:https://www.cnblogs.com/addNutri/p/4650482.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值