【Android】Android中Serializable接口的定義和使用
1阶界、序列化是干什么的虹钮?
簡(jiǎn)單說就是為了保存在內(nèi)存中的各種對(duì)象的狀態(tài)(也就是實(shí)例變量聋庵,不是方法),并且可以把保存的對(duì)象狀態(tài)再讀出來芙粱。雖然你可以用你自己的各種各樣的方法來保存object states祭玉,但是Java給你提供一種應(yīng)該比你自己好的保存對(duì)象狀態(tài)的機(jī)制,那就是序列化春畔。
2脱货、什么情況下需要序列化
a)當(dāng)你想把的內(nèi)存中的對(duì)象狀態(tài)保存到一個(gè)文件中或者數(shù)據(jù)庫中時(shí)候;
b)當(dāng)你想用套接字在網(wǎng)絡(luò)上傳送對(duì)象的時(shí)候律姨;
c)當(dāng)你想通過RMI傳輸對(duì)象的時(shí)候振峻;
3、Parcelable和Serializable的區(qū)別:
內(nèi)存間數(shù)據(jù)傳輸時(shí)推薦使用Parcelable,如activity間傳輸數(shù)據(jù)
保存到本地或者網(wǎng)絡(luò)傳輸時(shí)推薦使用Serializable
serialVersionUID 用來表明類的不同版本間的兼容性择份。
有兩種生成方式:?一個(gè)是默認(rèn)的1L扣孟;另一種是根據(jù)類名、接口名荣赶、成員方法及屬性等來生成一個(gè)64位的哈希字段 凤价。
Android中傳遞對(duì)象的三種方法
Android中,Activity和Fragment之間傳遞對(duì)象拔创,可以通過將對(duì)象序列化并存入Bundle或者Intent中進(jìn)行傳遞利诺,也可以將對(duì)象轉(zhuǎn)化為JSON字符串,進(jìn)行傳遞剩燥。
序列化對(duì)象可以使用Java的Serializable的接口慢逾、Parcelable接口。轉(zhuǎn)化成JSON字符串躏吊,可以使用Gson等庫氛改。
1.Serializable
Model
publicclassAuthorimplementsSerializable{
privateintid;
privateStringname;
//...
}
publicclassBookimplementsSerializable{
privateStringtitle;
privateAuthorauthor;
//...
}
傳遞數(shù)據(jù)
Bookbook=newBook();
book.setTitle("Java編程思想");
Authorauthor=newAuthor();
author.setId(1);
author.setName("Bruce?Eckel");
book.setAuthor(author);
Intentintent=newIntent(this,SecondActivity.class);
intent.putExtra("book",book);
startActivity(intent);
接收數(shù)據(jù)
Bookbook=(Book)getIntent().getSerializableExtra("book");
Log.d(TAG,"book?title->"+book.getTitle());
Log.d(TAG,"book?author?name->"+book.getAuthor().getName());
2.轉(zhuǎn)化為JSON字符串
Model
publicclassAuthor{
privateintid;
privateStringname;
//...
}
publicclassBook{
privateStringtitle;
privateAuthorauthor;
//...
}
傳遞數(shù)據(jù)
Bookbook=newBook();
book.setTitle("Java編程思想");
Authorauthor=newAuthor();
author.setId(1);
author.setName("Bruce?Eckel");
book.setAuthor(author);
Intentintent=newIntent(this,SecondActivity.class);
intent.putExtra("book",newGson().toJson(book));
startActivity(intent);
接收數(shù)據(jù)
StringbookJson=getIntent().getStringExtra("book");
Bookbook=newGson().fromJson(bookJson,Book.class);
Log.d(TAG,"book?title->"+book.getTitle());
Log.d(TAG,"book?author?name->"+book.getAuthor().getName());
3.使用Parcelable
實(shí)現(xiàn)Parcelable接口需要實(shí)現(xiàn)兩個(gè)方法
describeContents方法。內(nèi)容接口描述比伏,默認(rèn)返回0就可以;
writeToParcel方法。將傳遞的數(shù)據(jù)打包到Parcel容器中疆导。
除了要實(shí)現(xiàn)這兩個(gè)方法還必須創(chuàng)建一個(gè)Parcelable.Creator接口的實(shí)例赁项,用于讀取Parcel容器中的數(shù)據(jù)
Model
publicclassAuthorimplementsParcelable{
privateintid;
privateStringname;
//setter?&?getter...
@Override
publicintdescribeContents(){
return0;
}
@Override
publicvoidwriteToParcel(Parceldest,intflags){
//該方法將類的數(shù)據(jù)寫入外部提供的Parcel中.即打包需要傳遞的數(shù)據(jù)到Parcel容器保存,
//?以便從parcel容器獲取數(shù)據(jù)
dest.writeString(name);
dest.writeInt(id);
}
publicstaticfinalCreatorCREATOR=newCreator(){
@Override
publicAuthorcreateFromParcel(Parcelsource){
//從Parcel容器中讀取傳遞數(shù)據(jù)值澈段,封裝成Parcelable對(duì)象返回邏輯層悠菜。
Authorauthor=newAuthor();
author.setName(source.readString());
author.setId(source.readInt());
returnauthor;
}
@Override
publicAuthor[]newArray(intsize){
//創(chuàng)建一個(gè)類型為T,長(zhǎng)度為size的數(shù)組败富,僅一句話(return?new?T[size])即可悔醋。方法是供外部類反序列化本類數(shù)組使用。
returnnewAuthor[size];
}
};
}
publicclassBookimplementsParcelable{
privateStringtitle;
privateAuthorauthor;
//setter?&?getter...
@Override
publicintdescribeContents(){
return0;
}
@Override
publicvoidwriteToParcel(Parceldest,intflags){
dest.writeString(title);
dest.writeParcelable(author,flags);
}
publicstaticfinalCreatorCREATOR=newCreator(){
@Override
publicBookcreateFromParcel(Parcelsource){
Bookbook=newBook();
book.setTitle(source.readString());
book.setAuthor(source.readParcelable(Author.class.getClassLoader()));
returnbook;
}
@Override
publicBook[]newArray(intsize){
returnnewBook[0];
}
};
}
傳遞數(shù)據(jù)
Bookbook=newBook();
book.setTitle("Java編程思想");
Authorauthor=newAuthor();
author.setId(1);
author.setName("Bruce?Eckel");
book.setAuthor(author);
Intentintent=newIntent(this,SecondActivity.class);
intent.putExtra("book",book);
startActivity(intent);
接收數(shù)據(jù)
Bookbook=getIntent().getParcelableExtra("book");
Log.d(TAG,"book?title->"+book.getTitle());
Log.d(TAG,"book?author?name->"+book.getAuthor().getName());
4.性能分析
經(jīng)過測(cè)試兽叮,可以看出芬骄,通過轉(zhuǎn)換為字符串的速度是最慢的猾愿。Seralizable次之,Parcelable比Seralizable快10倍账阻。所以從性能上考 慮蒂秘,我們必定優(yōu)先選擇Parcelable。但是Parcelable有大量重復(fù)的模板代碼淘太,如何簡(jiǎn)化這些操作姻僧,將是下面主要講解的內(nèi)容。
5.簡(jiǎn)化Parcel操作
如果你使用android Studio 可以通過安裝android-parcelable-intellij-plugin插件蒲牧,或者自己配置模板進(jìn)行操作撇贺。
5.1 parceler
除了上面的操作,還有大量的第三方庫來簡(jiǎn)化Parcelable操作冰抢。當(dāng)然使用這些庫也許會(huì)降低Parcelable的性能松嘶。Parceler就是這樣一個(gè)庫。
Parceler使用非常簡(jiǎn)單晒屎,在定義Model時(shí)用@Parcel進(jìn)行注解喘蟆,在傳遞數(shù)據(jù)的時(shí)候使用Parcels的wrap方法來包裝成一個(gè)Parcelable對(duì)象。獲取數(shù)據(jù)時(shí)用Parcels的unwrap方法來獲取對(duì)象鼓鲁。
Model
@Parcel
publicclassAuthor{
intid;
Stringname;
//setter?&?getter...
}
@Parcel
publicclassBook{
Stringtitle;
Authorauthor;
//setter?&?getter
}
傳遞對(duì)象
Bookbook=newBook();
book.setTitle("Java編程思想");
Authorauthor=newAuthor();
author.setId(1);
author.setName("Bruce?Eckel");
book.setAuthor(author);
Intentintent=newIntent(this,SecondActivity.class);
intent.putExtra("book",Parcels.wrap(book));
startActivity(intent);
接收對(duì)象
Bookbook=Parcels.unwrap(getIntent().getParcelableExtra("book"));
Log.d(TAG,"book?title->"+book.getTitle());
Log.d(TAG,"book?author?name->"+book.getAuthor().getName());
除了Parceler之外蕴轨,還有如auto-parcel,ParcelableCodeGenerator,ParcelableGenerator等第三方庫,這里我將不進(jìn)行講解骇吭,有興趣的朋友橙弱,可以自行研究。