博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
浅谈Android中Serializable和Parcelable使用区别
阅读量:4684 次
发布时间:2019-06-09

本文共 4307 字,大约阅读时间需要 14 分钟。

Android中序列化有两种方式:Serializable以及Parcelable。其中Serializable是Java自带的,而Parcelable是安卓专有的。

一、Serializable序列化

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

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

我们编写Person类:

public class Person implements Serializable {    private static final long serialVersionUID = -3139325922167935911L;    //    private int age;    private String name;    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

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

MainActivity:

public class MainActivity extends Activity {    //    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //        Person p = new Person();        p.setAge(18);        p.setName("wanglei");        //        Intent i = new Intent(this, SecondActivity.class);        i.putExtra("person", p);        startActivity(i);            }}

 

 SecondActivity:

public class SecondActivity extends Activity {    //    private static final String TAG = "WL";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Intent intent = getIntent();        Person p=(Person) intent.getSerializableExtra("person");                Log.i(TAG, "age = "+p.getAge());        Log.i(TAG, "name = "+p.getName());    }}

以上代码及其简单了就不解释了,运行程序会看到如下打印:

以上就是Serializable方式序列化对象的举例,真的很简单,没有什么多余要解释的。

 

二、Parcelable序列化

arcel 在英文中有两个意思,其一是名词,为包裹,小包的意思; 其二为动词,意为打包,扎包。邮寄快递中的包裹也用的是这个词。Android采用这个词来表示封装消息数据。这个是通过IBinder通信的消息的载 体。需要明确的是Parcel用来存放数据的是内存(RAM),而不是永久性介质(Nand等)。

Parcelable,定义了将数据写入Parcel,和从Parcel中读出的接口。一个实体(用类来表示),如果需要封装到消息中去,就必须实现这一接口,实现了这一接口,该实体就成为“可打包的”了。

 

关与Parcelable方式实现序列化会比Serializable 方法麻烦一些,大体步骤如下:

1. 实现Parcelable接口

2. 覆写describeContents方法,默认返回0。
3. 覆写writeToParcel(Parcel dest, int flags)方法,指定写入Parcel类的数据。
4. 创建Parcelable.Creator静态对象,覆写方法createFromParcel(Parcel in)与newArray(int size)。

 

接口的定义如下:

public interface Parcelable {    //内容描述接口,基本不用管    public int describeContents();    //写入接口函数,打包    public void writeToParcel(Parcel dest, int flags);     //读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。因为实现类在这里还是不可知的,所以需要用到模板的方式,继承类名通过模板参数传入。    //为了能够实现模板参数的传入,这里定义Creator嵌入接口,内含两个接口函数分别返回单个和多个继承类实例。    public interface Creator
{ public T createFromParcel(Parcel source); public T[] newArray(int size); }}

下面定义了一个简单类People, 他需要把自身的数据,打入包中。 同时在消息的接收方需要通过People实现的Parcelable接口,将People重新构造出来。

People.javapublic class People implements Parcelable {    private String name = null;    private int age = 0;    private int sex = 0;// 0代表男、1代表女    public People() {        super();    }    public People(String name, int age, int sex) {        super();        this.name = name;        this.age = age;        this.sex = sex;    }    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeString(name);        dest.writeInt(age);        dest.writeInt(sex);    }    public static final Parcelable.Creator
CREATOR = new Creator
() { @Override public People[] newArray(int size) { return new People[size]; } @Override public People createFromParcel(Parcel source) { return new People(source.readString(), source.readInt(), source.readInt()); } };}

 

传递People

//发送 MainActivity.javaPeople people = new People();people.setName("张三");people.setSex(Integer.parseInt(0));people.setAge(Integer.parseInt(25));Intent intent = new Intent(MainActivity.this, ShowActivity.class);Bundle b = new Bundle();b.putParcelable("people", people);intent.putExtras(b);MainActivity.this.startActivity(intent);//接受 ShowActivity.javaif(getIntent().getExtras().containsKey("people")){  People people = getIntent().getExtras().getParcelable("people");}

 

 

参考:

https://www.cnblogs.com/xingfuzzhd/p/3454227.html

https://www.cnblogs.com/leipDao/p/8022063.html

 

转载于:https://www.cnblogs.com/longjunhao/p/10266351.html

你可能感兴趣的文章
P2709 小B的询问
查看>>
第三组的抓包作业
查看>>
ILNumerics项目的应用之线性方程
查看>>
django考点
查看>>
python-socket
查看>>
Android中intent如何传递自定义数据类型
查看>>
android基础---->子线程更新UI
查看>>
SharedPreferences
查看>>
转载 线程池之ThreadPool类与辅助线程 - <第二篇>
查看>>
js获取元素样式
查看>>
合并排序(C语言实现)
查看>>
sql 计算两时间或日期 的相差的 年、 月、 日、时、分、秒,年、月、日分别的提取...
查看>>
HDU 1176免费馅饼 DP数塔问题转化
查看>>
十进制二进制转换
查看>>
shiro实战系列(七)之Realm
查看>>
超像素、语义分割、实例分割、全景分割 傻傻分不清?
查看>>
HMM学习
查看>>
Mysql扩展之replication概述
查看>>
C++中数值的后缀
查看>>
EventModify
查看>>