簡述:Android中Parcelable介面

來源:互聯網
上載者:User

android整個上層java開發架構可以分為四個方面:介面(activity和appwidget)、訊息(Intent和Message)、服務(Service)和資料(Sqllite、Content Provider)。

開發要點摘記:

1、新的序列化方式:

   android提供了一種新的類型:Parcel。本類被用作封裝資料的容器,封裝後的資料可以通過Intent或IPC傳遞。

   除了基本類型以外,只有實現了Parcelable介面的類才能被放入Parcel中。

Parcelable實現要點:需要實現三個東西

1)writeToParcel 方法。該方法將類的資料寫入外部提供的Parcel中.聲明如下:

writeToParcel (Parcel dest, int flags) 具體參數含義見javadoc

2)describeContents方法。沒搞懂有什麼用,反正直接返回0也可以

3)自訂類型中必須含有一個名稱為CREATOR的靜態成員,該成員對象要求實現Parcelable.Creator介面及其方法。

簡而言之:通過writeToParcel將你的對象映射成Parcel對象,再通過createFromParcel將Parcel對象映射成你的對象。也可以將Parcel看成是一個流,通過

writeToParcel把對象寫到流裡面,在通過createFromParcel從流裡讀取對象,只不過這個過程需要你來實現,因此寫的順序和讀的順序必須一致。

靜態Parcelable.Creator介面,本介面有兩個方法:

createFromParcel(Parcel in) 實現從in中建立出類的執行個體的功能

newArray(int size) 建立一個類型為T,長度為size的數組,僅一句話(return new T[size])即可。估計本方法是供外部類還原序列化本類數組使用。

樣本:

    需求:我們經常需要在多個組件(activity或service)之間通過Intent傳遞一些資料,簡單類型(如數字、字串)的可以直接放入Intent。複雜類型(例如,J2ee中的Bean)的必須實現Parcelable介面。樣本如下:

package com.yang.domain; import android.os.Parcel;import android.os.Parcelable;  public class Person implements Parcelable {     //這裡定義了兩個變數來說明讀和寫的順序要一致       private Integer id;   private String name;     public Person() {   }     public Person(Integer id, String name) {                 this.id = id;        this.name = name;    }     public Integer getId() {        return id;    }     public void setId(Integer id) {       this.id = id;     }     public String getName() {        return name;    }       public void setName(String name) {        this.name = name;    }       @Override     public int describeContents() {        return 0;     }      @Override    public void writeToParcel(Parcel dest, int flags) {         // 把javanbean中的資料寫到Parcel。先寫id然後寫name           dest.writeInt(this.id);        dest.writeString(this.name);     }    // 添加一個靜態成員,名為CREATOR,該對象實現了Parcelable.Creator介面      public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {        @Override        public Person createFromParcel(Parcel source) {           // 從Parcel中讀取資料,返回person對象              return new Person(source.readInt(), source.readString());        }        @Override        public Person[] newArray(int size) {             return new Person[size];         }     };} 

註:Parcelable介面在Android當中有非常之多的子類,如下:

並且Intent當中也定義了很多關於Parcelable的get、set方法,如:

Intent      putExtra(String name,
Parcelable value)
Add extended data to the intent.


<T extends Parcelable> T getParcelableExtra(String name)
Retrieve extended data from the intent.

並且Intent本身也實現了Parcelable介面,因此在Android開發當中是非常推薦以Parcelable作為工具傳遞

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.