分享好友 移动开发首页 频道列表

Android Serializable

Android开发  2016-12-01 11:230

Serializable 是java提供的标准序列化接口,它是一个空接口,为对象提供标准的序列化和反序列化操作。

You can just implement Serializable interface and add override methods.The problem with this approach is that reflection is used and it is a slow process. This method create a lot of temporary objects and cause quite a bit of garbage collection. Serializable interface is easier to implement.

实现接口 Serializable 接口

publicclassMyObjectsimplementsSerializable{

privateString name;
privateintage;

publicArrayList<String> address;

publicMyObjects(String name,intage, ArrayList<String> address){
super();
this.name = name;
this.age = age;
this.address = address;
}

publicArrayList<String>getAddress(){
if(!(address ==null))
returnaddress;
else
returnnewArrayList<String>();
}

publicStringgetName(){
returnname;

}

publicStringgetAge(){
returnage;
}

}

传递 Serializable 对象:

//MyObjects instance
MyObjects mObjects = newMyObjects("name","age","Address array here");

//Passing MyObjects instance via intent
Intent mIntent = newIntent(FromActivity.this, ToActivity.class);
mIntent.putExtra("UniqueKey", mObjects);
startActivity(mIntent);


//Getting MyObjects instance
Intent mIntent = getIntent();
MyObjects workorder = (MyObjects) mIntent.getSerializableExtra("UniqueKey");

Serializable 的实现原理是将需要序列化的对象写入文件内,反序列化的时候再从文件内读取数据组成对象。这个过程中会有一个 serialVersionID 来标记当前序列化文件的版本,反序列化的时候会

检测当前类的serialVersionID和文件中的serialVersionID是否一样,如果一样,那么就说明当前类和序列化的对象的版本相同(即使类不同,也能恢复大部分数据),否则就说明当前类和序列化对象有所不同,可能是增减了成员变量之类的变化,这个时候反序列化程序就会crash。

另外,序列化和反序列化的过程是通过 writeObjectreadObject 来实现的,可以重写他们改变系统的默认序列化和反序列化过程。

ObjectOutputStream 序列化

//序列化
User user = new User(0,"jack",true);
ObjectOutputStream out = new ObjectOutputStream(
 new FileOutputStream("cache.txt"));
 out.writeObject(user);
 out.close();

ObjectInputStream 反序列化

//反序列化
ObjectInputStream in = newObjectInputStream(
newFileInputStream("cache.txt"));
User user = (User) in.readObject();
in.close();

Parcelable

android中提供的序列化接口,实现这个接口以后就可以实现序列化并且可以通过Intent和Binder传递。

Parcelable process is much faster than serializable. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. It also stands to reason that the code has been heavily optimized for this purpose.

//MyObjects Parcelable class

importjava.util.ArrayList;

importandroid.os.Parcel;
importandroid.os.Parcelable;

publicclassMyObjectsimplementsParcelable{

privateintage;
privateString name;

privateArrayList<String> address;

publicMyObjects(String name,intage, ArrayList<String> address){
this.name = name;
this.age = age;
this.address = address;

}

publicMyObjects(Parcel source){
 age = source.readInt();
 name = source.readString();
 address = source.createStringArrayList();
}

@Override
publicintdescribeContents(){
return0;
}

@Override
publicvoidwriteToParcel(Parcel dest,intflags){
 dest.writeInt(age);
 dest.writeString(name);
 dest.writeStringList(address);

}

publicintgetAge(){
returnage;
}

publicStringgetName(){
returnname;
}

publicArrayList<String>getAddress(){
if(!(address ==null))
returnaddress;
else
returnnewArrayList<String>();
}

publicstaticfinalCreator<MyObjects> CREATOR =newCreator<MyObjects>() {
@Override
publicMyObjects[] newArray(intsize) {
returnnewMyObjects[size];
 }

@Override
publicMyObjectscreateFromParcel(Parcel source){
returnnewMyObjects(source);
 }
};

}
MyObjects mObjects = newMyObjects("name","age","Address array here");

//Passing MyOjects instance
Intent mIntent = newIntent(FromActivity.this, ToActivity.class);
mIntent.putExtra("UniqueKey", mObjects);
startActivity(mIntent);

//Getting MyObjects instance
Intent mIntent = getIntent();
MyObjects workorder = (MyObjects) mIntent.getParcelable("UniqueKey");

//You can pass Arraylist of Parceble obect as below

//Array of MyObjects
ArrayList<MyObjects> mUsers;

//Passing MyOjects instance
Intent mIntent = newIntent(FromActivity.this, ToActivity.class);
mIntent.putParcelableArrayListExtra("UniqueKey", mUsers);
startActivity(mIntent);


//Getting MyObjects instance
Intent mIntent = getIntent();
ArrayList<MyObjects> mUsers = mIntent.getParcelableArrayList("UniqueKey");

Parcelable and Serializable 比较

  1. Parcelable is faster than serializable interface
  2. Parcelable interface takes more time for implemetation compared to serializable interface
  3. serializable interface is easier to implement
  4. serializable interface create a lot of temporary objects and cause quite a bit of garbage collection
  5. Parcelable array can be pass via Intent in android

If you want to be a good citizen, take the extra time to implement Parcelable since it will perform 10 times faster and use less resources.

However, in most cases, the slowness of Serializable won’t be noticeable. Feel free to use it but remember that serialization is an expensive operation so keep it to a minimum.

If you are trying to pass a list with thousands of serialized objects, it is possible that the whole process will take more than a second. It can make transitions or rotation from portrait to landscape feel very sluggish.

References

查看更多关于【Android开发】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
Supporting Multiple Screens
术语和概念Screen size 屏幕尺寸又称「屏幕大小」,是屏幕对角线的物理尺寸。单位英寸 inch,比如 Samsung Note4 是 5.7 英寸。Resolution 屏幕分辨率屏幕纵横方向上物理像素的总数,比如 Samsung Note4 是 2560x1440,表示纵向有 2560 个像素,横向有 1440

0评论2017-02-05363

Android插件化(4):OpenAtlasの插件的卸载与更新
如果看过我的前两篇博客Android插件化(2):OpenAtlas插件安装过程分析和Android插件化(3):OpenAtlas的插件重建以及使用时安装,就知道在插件的安装过程中OpenAtlas做了哪些事,那么插件的卸载就只需要把持久化和内存中的内容移除即可。1.插件的卸载插件卸载的

0评论2017-02-05229

个人简历
吴朝晖/男/1993.1本科/南京师范大学中北学院信息系工作年限:1年以内技术博客:wuzhaohui026.github.ioGitHub:https://github.com/wuzhaohui026期望职位:Android开发(初级Android工程师)期望薪资:税前月薪5.5k~7k期望城市:常州工作经历常州慧展信息科技有

0评论2017-02-05126

Android插件化(五):OpenAtlasの四大组件的Hack
引言到目前为止,我们已经分析了OpenAtlas中插件的安装,卸载,更新,以及安装好插件之后组件类的加载过程,但是对于这些是如何引发的还不知道,比如,在宿主的一个Activit中调用startActivity()跳转到插件中的一个Activity,如何判断这个Activity在的插件是否

0评论2017-02-0598

更多推荐