Android中Parcelable介面的使用

來源:互聯網
上載者:User

標籤:

  在做開發的過程中,序列化是非常常見的。比如要將對象儲存本地磁碟或者在網路上傳輸等。實現序列化有兩種方式,一種是實現Serializable介面,第二種是實現Parcelable。

Serializable與Parcelable的區別

  1、Serializable是JDK提供的介面,而Parcelable是Android SDK提供的。

  2、Serializable序列化是基於磁碟的,而Parcelable是基於記憶體的。在記憶體中讀寫肯定效率要高於磁碟,所以Android中跨進程傳遞對象都是使用Parcelable。

Parcelable介面定義

 1 public interface Parcelable { 2     //內容描述介面,基本不用管 3     public int describeContents(); 4     //寫入介面函數,打包 5     public void writeToParcel(Parcel dest, int flags); 6      //讀取介面,目的是要從Parcel中構造一個實現了Parcelable的類的執行個體處理。因為實作類別在這裡還是不可知的,所以需要用到模板的方式,繼承類名通過模板參數傳入。 7     //為了能夠實現模板參數的傳入,這裡定義Creator嵌入介面,內含兩個介面函數分別返回單個和多個繼承類執行個體。 8     public interface Creator<T> { 9            public T createFromParcel(Parcel source);10            public T[] newArray(int size);11        }

  從parcelable介面定義中,我們可以看到,實現parcelable介面,需要我們實現下面幾個方法:

     1.describeContents方法。內容介面描述,預設返回0就可以;

     2.writeToParcel 方法。該方法將類的資料寫入外部提供的Parcel中.即打包需要傳遞的資料到Parcel容器儲存,以便從parcel容器擷取資料,該方法聲明如下:

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

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

        createFromParcel(Parcel in)  從Parcel容器中讀取傳遞資料值,封裝成Parcelable對象返回邏輯層。

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

 

Parcelable的使用

 1 public class AppContent implements Serializable, Parcelable { 2     //應用程式名稱字 3     private String name; 4     //應用下載連結 5     private String url; 6  7     private int downloadPercent = 0; 8  9     private Status status = Status.PENDING;10 11     public AppContent(String name, String url) {12         this.name = name;13         this.url = url;14     }15 16     public String getName() {17         return name;18     }19 20     public void setName(String name) {21         this.name = name;22     }23 24     public String getUrl() {25         return url;26     }27 28     public void setUrl(String url) {29         this.url = url;30     }31 32     public int getDownloadPercent() {33         return downloadPercent;34     }35 36     public void setDownloadPercent(int downloadPercent) {37         this.downloadPercent = downloadPercent;38     }39 40     public Status getStatus() {41         return status;42     }43 44     public void setStatus(Status status) {45         this.status = status;46     }47 48     @Override49     public String toString() {50         return name;51     }52 53     @Override54     public int describeContents() {55         return 0;56     }57 58     //實現Parcel介面必須覆蓋實現的方法59     @Override60     public void writeToParcel(Parcel dest, int flags) {61         /*將AppContent的成員寫入Parcel,62          * 註:Parcel中的資料是按順序寫入和讀取的,即先被寫入的就會先被讀取出來63          */64         dest.writeString(name);65         dest.writeString(url);66         dest.writeInt(downloadPercent);67         dest.writeValue(status);68     }69 70     //該靜態域是必須要有的,而且名字必須是CREATOR,否則會出錯71     public static final Parcelable.Creator<AppContent> CREATOR =72             new Parcelable.Creator<AppContent>() {73 74                 @Override75                 public AppContent createFromParcel(Parcel source) {76                     //從Parcel讀取通過writeToParcel方法寫入的AppContent的相關成員資訊77                     String name = source.readString();78                     String url = source.readString();79                     int downloadPercent = source.readInt();80                     Status status = (Status)source.readValue(new ClassLoader(){});81                     AppContent appContent = new AppContent(name, url);82                     appContent.setDownloadPercent(downloadPercent);83                     appContent.setStatus(status);84                     //更加讀取到的資訊,建立返回Person對象85                     return appContent;86                 }87 88                 @Override89                 public AppContent[] newArray(int size)90                 {91                     // TODO Auto-generated method stub92                     //返回AppContent對象數組93                     return new AppContent[size];94                 }95             };96 97 }

  通過Intent進行傳遞:

1    Intent intent = new Intent(Constants.DOWNLOAD_MSG);2    Bundle bundle = new Bundle();3    bundle.putParcelable("appContent", appContent);4    intent.putExtras(bundle);

 

  參考:http://www.tuicool.com/articles/MJzAZn,感謝作者。

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.