国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發之Parcel機制實例分析

2020-04-11 11:32:44
字體:
來源:轉載
供稿:網友

本文實例講述了Android開發之Parcel機制。分享給大家供大家參考。具體分析如下:

在java中,有序列化機制。但是在安卓設備上,由于內存有限,所以設計了新的序列化機制。

Container for a message (data and object references) that can be sent through an IBinder.  A Parcel can contain both flattened data that will be unflattened on the other side of the IPC (using the various methods here for writing specific types, or the generalParcelable interface), and references to liveIBinder objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel.

Parcel is not a general-purpose serialization mechanism.  This class (and the correspondingParcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport.  As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

從上面的官方解釋可以看到,Parcel主要就是用來序列化,在一端編碼,在另外一端進行解碼。

本質上把它當成一個Serialize就可以了,只是它是在內存中完成的序列化和反序列化,利用的是連續的內存空間,因此會更加高效。

我們接下來要說的是Parcel類如何應用。就應用程序而言,最常見使用Parcel類的場景就是在Activity間傳遞數據。沒錯,在Activity間使用Intent傳遞數據的時候,可以通過Parcelable機制傳遞復雜的對象。

具體例子可以參見這里,寫的很好。

在實現Parcelable接口的時候,必須實現其中的兩個方法并且定義一個CREATOR:

@Override public int describeContents() {    return 0; } @Override public void writeToParcel(Parcel dest, int flags) {    dest.writeInt(color); }

其中,writeToParcel方法定義了怎么向序列化中寫入該類對象的信息。

CREATOR對象中定義了兩個函數:

public MyColor createFromParcel(Parcel in) {  return new MyColor(in);}public MyColor[] newArray(int size) {  return new MyColor[size];}

其中,createFromParcel方法告訴平臺如何從已經序列化的對象中構建該類的實例。newArray方法的作用不明。實現于Parcelable接口的CREATOR成員的createFromParcel方法用于告訴平臺如何從包裹里創建該類的實例,而writeToParcel方法則用于告訴平臺如何將該類的實例存儲到包裹中。通過這種約定,平臺就知道怎么序列化和反序列化了。

希望本文所述對大家的Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福海县| 余姚市| 洪湖市| 内黄县| 西平县| 大城县| 南溪县| 如东县| 西贡区| 辛集市| 霸州市| 昭苏县| 威宁| 杭锦旗| 扎鲁特旗| 青海省| 乡城县| 竹溪县| 九龙城区| 台山市| 嘉荫县| 隆子县| 高要市| 九寨沟县| 灌云县| 太白县| 庐江县| 北京市| 荣昌县| 乐业县| 陇南市| 威远县| 于田县| 甘洛县| 霍山县| 宾川县| 西青区| 宣化县| 舟山市| 新野县| 石阡县|