安卓实现序列化之Parcelable接口
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 安卓實現序列化之Parcelable接口
1.實現序列化的方法:
? ? ? ?Android中實現序列化有兩個選擇:一是實現Serializable接口(是JavaSE本身就支持的)
。一是實現Parcelable接口(是Android特有功能,效率比實現Serializable接口高效,可用
于Intent數據傳遞。也能夠用于進程間通信(IPC))。實現Serializable接口很easy。聲
明一下就能夠了,而實現Parcelable接口略微復雜一些。但效率更高,推薦用這樣的方法提高性能。
注:Android中Intent傳遞對象有兩種方法:一是Bundle.putSerializable(Key,Object)。還有一種是
Bundle.putParcelable(Key,Object)。
當然這些Object是有一定的條件的,前者是實現了Serializable
接口,而后者是實現了Parcelable接口。
2.為什么要實現序列化?
? ??永久性保存對象,保存對象的字節序列到本地文件里;通過序列化對象在網絡中傳遞對象;
通過序列化在進程間傳遞對象。
3.選擇序列化方法的原則
1)在使用內存的時候。Parcelable比Serializable性能高。所以推薦使用Parcelable。
2)Serializable在序列化的時候會產生大量的暫時變量,從而引起頻繁的GC。
3)Parcelable不能使用在要將數據存儲在磁盤上的情況,由于Parcelable不能非常好的保證
? ? ?數據的持續性在外界有變化的情況下。雖然Serializable效率低點,但此時還是建議使
? ? ?用Serializable 。
4.應用場景:
? ? ?須要在多個部件(Activity或Service)之間通過Intent傳遞一些數據,簡單類型(如:數字、
字符串)的能夠直接放入Intent。復雜類型必須實現Parcelable接口。
5.Parcelable接口定義
public interface Parcelable
{
? ? //內容描寫敘述接口,基本不用管
? ? public int describeContents();
? ? //寫入接口函數,打包
? ? public void writeToParcel(Parcel dest, int flags);
? ? ?//讀取接口,目的是要從Parcel中構造一個實現了Parcelable的類的實例處理。由于
實現類在這里還是不可知的,所以須要用到模板的方式。繼承類名通過模板參數傳入。
? ? //為了可以實現模板參數的傳入,這里定義Creator嵌入接口,內含兩個接口函數分別
返回單個和多個繼承類實例。
public interface Creator<T>
{
? ? ? ? ? ?public T createFromParcel(Parcel source);
? ? ? ? ? ?public T[] newArray(int size);
}
}
6.實現Parcelable步驟
1)implements Parcelable
2)重寫writeToParcel方法,將你的對象序列化為一個Parcel對象,即:將類的數據寫入
外部提供的Parcel中,打包須要傳遞的數據到Parcel容器保存,以便從 Parcel容器獲取數據
3)重寫describeContents方法。內容接口描寫敘述,默認返回0就能夠
4)實例化靜態內部對象CREATOR實現接口Parcelable.Creator
public static final Parcelable.Creator<T> CREATOR
注:當中public static final一個都不能少。內部對象CREATOR的名稱也不能改變,
必須所有大寫。需重寫本接口中的兩個方法:createFromParcel(Parcel in) 實現從
Parcel容器中讀取傳遞數據值,封裝成Parcelable對象返回邏輯層,newArray(int size)
創建一個類型為T。長度為size的數組。僅一句話就可以(return new T[size]),供外部
類反序列化本類數組使用。
簡而言之:通過writeToParcel將你的對象映射成Parcel對象,再通過createFromParcel
將Parcel對象映射成你的對象。
也能夠將Parcel看成是一個流,通過writeToParcel把對象
寫到流里面,在通過createFromParcel從流里讀取對象,僅僅只是這個過程須要你來實現,
因此寫的順序和讀的順序必須一致。
代碼例如以下:
public class MyParcelable implements Parcelable?
{
? ? ?private int mData;
? ? ?public int describeContents()?
? ? ?{
? ? ? ? ?return 0;
? ? ?}
? ? ?public void writeToParcel(Parcel out, int flags)?
? ? ?{
? ? ? ? ?out.writeInt(mData);
? ? ?}
? ? ?public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>()?
? ? ?{
? ? ? ? ?public MyParcelable createFromParcel(Parcel in)?
? ? ? ? ?{
? ? ? ? ? ? ?return new MyParcelable(in);
? ? ? ? ?}
? ? ? ? ?public MyParcelable[] newArray(int size)?
? ? ? ? ?{
? ? ? ? ? ? ?return new MyParcelable[size];
? ? ? ? ?}
? ? ?};
? ? ?
? ? ?private MyParcelable(Parcel in)?
? ? ?{
? ? ? ? ?mData = in.readInt();
? ? ?}
?}
7.Serializable實現與Parcelabel實現的差別
1)Serializable的實現。僅僅須要implements ?Serializable 就可以。這僅僅是給對象打
了一個標記。系統會自己主動將其序列化。
2)Parcelabel的實現,不僅須要implements ?Parcelabel,還須要在類中加入一個
靜態成員變量CREATOR。這個變量須要實現 Parcelable.Creator 接口。
兩者代碼比較:
1)創建Person類,實現Serializable
public class Person implements Serializable
{
? ? private static final long serialVersionUID = -7060210544600464481L;
? ? private String name;
? ? private int age;
? ??
? ? public String getName()
? ? {
? ? ? ? return name;
? ? }
? ??
? ? public void setName(String name)
? ? {
? ? ? ? this.name = name;
? ? }
? ??
? ? public int getAge()
? ? {
? ? ? ? return age;
? ? }
? ??
? ? public void setAge(int age)
? ? {
? ? ? ? this.age = age;
? ? }
}
2)創建Book類,實現Parcelable
public class Book implements Parcelable
{
? ? private String bookName;
? ? private String author;
? ? private int publishDate;
? ??
? ? public Book()
? ? {
? ? ? ??
? ? }
? ??
? ? public String getBookName()
? ? {
? ? ? ? return bookName;
? ? }
? ??
? ? public void setBookName(String bookName)
? ? {
? ? ? ? this.bookName = bookName;
? ? }
? ??
? ? public String getAuthor()
? ? {
? ? ? ? return author;
? ? }
? ??
? ? public void setAuthor(String author)
? ? {
? ? ? ? this.author = author;
? ? }
? ??
? ? public int getPublishDate()
? ? {
? ? ? ? return publishDate;
? ? }
? ??
? ? public void setPublishDate(int publishDate)
? ? {
? ? ? ? this.publishDate = publishDate;
? ? }
? ??
? ? @Override
? ? public int describeContents()
? ? {
? ? ? ? return 0;
? ? }
? ??
? ? @Override
? ? public void writeToParcel(Parcel out, int flags)
? ? {
? ? ? ? out.writeString(bookName);
? ? ? ? out.writeString(author);
? ? ? ? out.writeInt(publishDate);
? ? }
? ??
? ? public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>()
? ? {
? ? ? ? @Override
? ? ? ? public Book[] newArray(int size)
? ? ? ? {
? ? ? ? ? ? return new Book[size];
? ? ? ? }
? ? ? ??
? ? ? ? @Override
? ? ? ? public Book createFromParcel(Parcel in)
? ? ? ? {
? ? ? ? ? ? return new Book(in);
? ? ? ? }
? ? };
? ??
? ? public Book(Parcel in)
? ? {
? ? ? ? bookName = in.readString();
? ? ? ? author = in.readString();
? ? ? ? publishDate = in.readInt();
? ? }
}
轉載于:https://www.cnblogs.com/lytwajue/p/7098094.html
總結
以上是生活随笔為你收集整理的安卓实现序列化之Parcelable接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 租赁备案和居住证的区别?
- 下一篇: W18K是什么意思?