淺談Android序列化


序列化原因

序列化的原因基本可以歸納為以下三種情況:

  1. 永久性保存對象,保存對象的字節序列到本地文件中;
  2. 對象在網絡中傳遞;
  3. 對象在IPC間傳遞。

序列化方法

在Android系統中關於序列化的方法一般有兩種,分別是實現Serializable接口和Parcelable接口,其中Serializable接口是來自Java中的序列化接口,而Parcelable是Android自帶的序列化接口。
上述的兩種序列化接口都有各自不同的優缺點,我們在實際使用時需根據不同情況而定。

  1. Serializable在序列化的時候會產生大量的臨時變量,從而引起頻繁的GC,而相比之下Parcelable的性能更高(畢竟是Android自帶的),所以當在使用內存時(如:序列化對象在網絡中傳遞對象或序列化在進程間傳遞對象),更推薦使用Parcelable接口。
  2. 但Parcelable有個明顯的缺點:不能能使用在要將數據存儲在磁盤上的情況(如:永久性保存對象,保存對象的字節序列到本地文件中),因為Parcel本質上為了更好的實現對象在IPC間傳遞並不是一個通用的序列化機制,當改變任何Parcel中數據的底層實現都可能導致之前的數據不可讀取,所以此時還是建議使用Serializable 。

代碼實現

Serializable接口的實現及使用

  • Serializable的接口實現很簡單,只需讓需要序列化的類繼承Serializable 即可,系統會自動將其序列化,具體代碼如下:

     public class Book implements Serializable {
     	private static final long serialVersionUID = 21455356667888L;
     	private String mName;
     	private String mPrice;
    
     	public String getmName() {
     		return mName;
     	}
    
     	public void setmName(String mName) {
     		this.mName = mName;
     	}
    
     	public String getmPrice() {
     		return mPrice;
     	}
    
     	public void setmPrice(String mPrice) {
     		this.mPrice = mPrice;
     	}
    
     }
    
  • 在Activity中使用方法:

     // serializable對象傳遞方法
     public void setSerializableMethod() {
     	Book book = new Book();
     	book.setmName("王海康");
     	book.setmPrice("20$");
     	Intent intent = new Intent(this, BookTest.class);
     	Bundle bundle = new Bundle();
     	bundle.putSerializable(SER_KEY, book);
     	intent.putExtras(bundle);
     	startActivity(intent);
     }
     
     // serializable對象獲取方法
     public Book getSerializableMethod(){
     	Book mBook = (Book )getIntent().getSerializableExtra(SER_KEY);
     	return mBook;
     }
    

Parcelable接口的實現及使用

  • 實現Parcelable接口主要可以分為一下幾步:
    1)implements Parcelable。
    2)重寫writeToParcel方法,將你的對象序列化為一個Parcel對象,即:將類的數據寫入外部提供的Parcel中,打包需要傳遞的數據到Parcel容器保存,以便從Parcel容器獲取數據。
    3)重寫describeContents方法,內容接口描述,默認返回0即可。
    4)實例化靜態內部對象CREATOR實現接口Parcelable.Creator 。
    注意:若將Parcel看成是一個流,則先通過writeToParcel把對象寫到流里面,再通過createFromParcel從流里讀取對象,因此類實現的寫入順序和讀出順序必須一致。
    具體實現代碼如下:

     public class Person implements Parcelable {
     	private String mName;
     	private String mSex;
     	private int mAge;
    
     	public String getmName() {
     		return mName;
     	}
    
     	public void setmName(String mName) {
     		this.mName = mName;
     	}
    
     	public String getmSex() {
     		return mSex;
     	}
    
     	public void setmSex(String mSex) {
         	this.mSex = mSex;
     	}
    
     	public int getmAge() {
     		return mAge;
     	}
    
     	public void setmAge(int mAge) {
     		this.mAge = mAge;
     	}
    
     	@Override
     	public int describeContents() {
     		return 0;
     	}
    
     	@Override
     	public void writeToParcel(Parcel dest, int flags) {
     		dest.writeString(mName);
     		dest.writeString(mSex);
     		dest.writeInt(mAge);
     	}
    
     	public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
     	    
     		@Override
     		public Person createFromParcel(Parcel source) {
     			Person person = new Person();
     			person.mName = source.readString();
     			person.mSex = source.readString();
     			person.mAge = source.readInt();
     			return person;
     		}
    
     	    //供反序列化本類數組時調用的
     		@Override
     		public Person[] newArray(int size) {
     			return new Person[size];
     		}
     	};
    
  • 在Activity中使用方法:

    1)傳遞單一對象,具體代碼如下:

     // parcelable對象傳遞方法
     public void setParcelableMethod() {
     	Person person = new Person();
     	person.setmName("王海康");
     	person.setmSex("男");
     	person.setmAge(45);
     	Intent intent = new Intent(this, PersonTest.class);
     	Bundle bundle = new Bundle();
     	bundle.putParcelable(PAR_KEY, person);
     	intent.putExtras(bundle);
     	startActivity(intent);
     }
    
     // parcelable對象獲取方法
     public Person getParcelableMethod(){
     	Person mPerson = (Person)getIntent().getParcelableExtra(PAR_KEY);
     	return mPerson;
     }
    

    2)傳遞對象列表,具體代碼如下:
    需要注意的是,若List personList = new ArrayList ();則會報錯,因為下面調用的putParcelableArrayList()函數其中一個參數的類型為ArrayList。

     // parcelable對象List傳遞方法
     public void setParcelableListMethod() {
     	ArrayList<Person> personList = new ArrayList<Person>();
     	Person person1 = new Person();
     	person1.setmName("王海康");
     	person1.setmSex("男");
     	person1.setmAge(45);
     	personList.add(person1);
     	Person person2 = new Person();
     	person2.setmName("薛岳");
     	person2.setmSex("男");
     	person2.setmAge(15);
     	personList.add(person2);
     	Intent intent = new Intent(this, PersonTest.class);
     	Bundle bundle = new Bundle();
     	bundle.putParcelableArrayList(PAR_LIST_KEY, personList);
     	intent.putExtras(bundle);
     	startActivity(intent);
     }
     
     // parcelable對象獲取方法
     public ArrayList<Person> getParcelableMethod(){
     	ArrayList<Person> mPersonList = getIntent().getParcelableExtra(PAR_LIST_KEY);
     return mPersonList;
     }
    

    3)最后介紹一個投機取巧的方法:
    不用繼承Parcelable或Serializable方法即可實現IPC中對象的傳遞。這種方法的實現原理不是很明白,只知道代碼中new ArrayList()返回的其實是一個EmptyArray.OBJECT數組,不過我感覺應該還是系統調用Serializable進行序列化的,如果各位讀者有好的想法,歡迎告知。
    具體代碼如下:

     //對象List傳遞
     public void setObjectMethod(){
         ......(省略)
         ArrayList list = new ArrayList();
         //ObjectList為某一對象列表
         list.add(ObjectList);
         bundle.putParcelableArrayList(PAR_LIST_KEY, list);
         intent.putExtras(bundle);
     	startActivity(intent);
     }
    
     //獲取對象List
     ArrayList list = bundle.getParcelableArrayList("list");
     //強轉成你自己定義的list,這樣ObjectList就是你傳過來的那個list了。
     ObjectList= (List<Object>) list.get(0);
    

參考資料:
1.Android Parcel官方文檔
2.Android Serializable 和 Parcelable 區別
3.Android中Parcelable接口用法

查看原文:http://www.xyczero.com/blog/article/8/.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM