欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中的序列化淺析

 更新時(shí)間:2015年03月23日 09:17:48   投稿:junjie  
這篇文章主要介紹了Android中的序列化淺析,本文講解了序列化原因、序列化方法和代碼實(shí)現(xiàn)等內(nèi)容,需要的朋友可以參考下

序列化原因

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

1.永久性保存對(duì)象,保存對(duì)象的字節(jié)序列到本地文件中;
2.對(duì)象在網(wǎng)絡(luò)中傳遞;
3.對(duì)象在IPC間傳遞。

序列化方法

在Android系統(tǒng)中關(guān)于序列化的方法一般有兩種,分別是實(shí)現(xiàn)Serializable接口和Parcelable接口,其中Serializable接口是來自Java中的序列化接口,而Parcelable是Android自帶的序列化接口。

上述的兩種序列化接口都有各自不同的優(yōu)缺點(diǎn),我們?cè)趯?shí)際使用時(shí)需根據(jù)不同情況而定。


1.Serializable在序列化的時(shí)候會(huì)產(chǎn)生大量的臨時(shí)變量,從而引起頻繁的GC,而相比之下Parcelable的性能更高(畢竟是Android自帶的),所以當(dāng)在使用內(nèi)存時(shí)(如:序列化對(duì)象在網(wǎng)絡(luò)中傳遞對(duì)象或序列化在進(jìn)程間傳遞對(duì)象),更推薦使用Parcelable接口。

2.但Parcelable有個(gè)明顯的缺點(diǎn):不能能使用在要將數(shù)據(jù)存儲(chǔ)在磁盤上的情況(如:永久性保存對(duì)象,保存對(duì)象的字節(jié)序列到本地文件中),因?yàn)镻arcel本質(zhì)上為了更好的實(shí)現(xiàn)對(duì)象在IPC間傳遞,并不是一個(gè)通用的序列化機(jī)制,當(dāng)改變?nèi)魏蜳arcel中數(shù)據(jù)的底層實(shí)現(xiàn)都可能導(dǎo)致之前的數(shù)據(jù)不可讀取,所以此時(shí)還是建議使用Serializable 。

代碼實(shí)現(xiàn)

Serializable接口的實(shí)現(xiàn)及使用

Serializable的接口實(shí)現(xiàn)很簡(jiǎn)單,只需讓需要序列化的類繼承Serializable 即可,系統(tǒng)會(huì)自動(dòng)將其序列化,具體代碼如下:

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對(duì)象傳遞方法
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對(duì)象獲取方法
public Book getSerializableMethod(){
  Book mBook = (Book )getIntent().getSerializableExtra(SER_KEY);
  return mBook;
}

Parcelable接口的實(shí)現(xiàn)及使用

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

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;
    }

    //供反序列化本類數(shù)組時(shí)調(diào)用的
    @Override
    public Person[] newArray(int size) {
      return new Person[size];
    }
  };

在Activity中使用方法:

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

// parcelable對(duì)象傳遞方法
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對(duì)象獲取方法
public Person getParcelableMethod(){
  Person mPerson = (Person)getIntent().getParcelableExtra(PAR_KEY);
  return mPerson;
}

2)傳遞對(duì)象列表,具體代碼如下:
需要注意的是,若List personList = new ArrayList();則會(huì)報(bào)錯(cuò),因?yàn)橄旅嬲{(diào)用的putParcelableArrayList()函數(shù)其中一個(gè)參數(shù)的類型為ArrayList。

// parcelable對(duì)象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對(duì)象獲取方法
public ArrayList<Person> getParcelableMethod(){
  ArrayList<Person> mPersonList = getIntent().getParcelableExtra(PAR_LIST_KEY);
return mPersonList;
}

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

//對(duì)象List傳遞
public void setObjectMethod(){
  ......(省略)
  ArrayList list = new ArrayList();
  //ObjectList為某一對(duì)象列表
  list.add(ObjectList);
  bundle.putParcelableArrayList(PAR_LIST_KEY, list);
  intent.putExtras(bundle);
  startActivity(intent);
}

//獲取對(duì)象List
ArrayList list = bundle.getParcelableArrayList("list");
//強(qiáng)轉(zhuǎn)成你自己定義的list,這樣ObjectList就是你傳過來的那個(gè)list了。
ObjectList= (List<Object>) list.get(0);

相關(guān)文章

最新評(píng)論