Android開發:什麼是Parcel(2)

來源:互聯網
上載者:User

上回書解釋了IBinder,這回詳細解釋一下Parcel,以下是對android sdk 文檔的翻議:
Parcel是一個容器,它主要用於儲存序列化資料,然後可以通過Binder在進程間傳遞這些資料(要瞭解為什麼要序列化,請參考:http://blog.csdn.net/nkmnkm/archive/2011/05/28/6451699.aspx)。Parcel可以包含未經處理資料類型(用各種對應的方法寫入,比如writeInt(),writeFloat()等),可以包含Parcelable對象,它還包含了一個活動的IBinder對象的引用,這個引用導致另一端接收到一個指向這個IBinder的代理IBinder。

註:Parcel不是一般目的的序列化機制。這個類被設計用於高效能的IPC傳輸。因此不適合把Parcel寫入永久化儲存中,因為Parcel中的資料類型的實現的改變會導致舊版的資料不可讀。

  Parcel的一坨一坨的API用於解決不同類型資料的讀寫。這些函數們主要有六種類型。

1原始類

這類方法們主要讀寫未經處理資料類型。它們是:writeByte(byte), readByte(), writeDouble(double), readDouble(), writeFloat(float), readFloat(), writeInt(int), readInt(), writeLong(long), readLong(), writeString(String), readString(). 大多數其它資料的操作都是基於這些方法。

2原始數組類

這類方法用於讀寫未經處理資料組成的數組。在向數組寫資料時先寫入數組的長度再寫入資料。讀數組的方法可以將資料讀到已存在的數組中,也可以建立並返回一個新數組。它們是:

  • writeBooleanArray(boolean[]), readBooleanArray(boolean[]), createBooleanArray()
    writeByteArray(byte[]), writeByteArray(byte[], int, int), readByteArray(byte[]), createByteArray()
    writeCharArray(char[]), readCharArray(char[]), createCharArray()
    writeDoubleArray(double[]), readDoubleArray(double[]), createDoubleArray()
    writeFloatArray(float[]), readFloatArray(float[]), createFloatArray()
    writeIntArray(int[]), readIntArray(int[]), createIntArray()
    writeLongArray(long[]), readLongArray(long[]), createLongArray()
    writeStringArray(String[]), readStringArray(String[]), createStringArray().
    writeSparseBooleanArray(SparseBooleanArray), readSparseBooleanArray().

3 Parcelable類
Parcelable為對象從Parcel中讀寫自己提供了極其高效的協議。你可以使用直接的方法 writeParcelable(Parcelable, int) 和 readParcelable(ClassLoader) 或 writeParcelableArray(T[], int) and readParcelableArray(ClassLoader) 進行讀寫。這些方法們把類的資訊和資料都寫入Parcel,以使將來能使用合適的類裝載器重新構造類的執行個體。

還有一些方法提供了更高效的操作Parcelable們的途徑,它們是:writeTypedArray(T[], int), writeTypedList(List), readTypedArray(T[], Parcelable.Creator) and readTypedList(List, Parcelable.Creator)。這些方法不會寫入類的資訊,取而代之的是:讀取時必須能知道資料屬於哪個類並傳入正確的Parcelable.Creator來建立對象而不是直接構造新對象。(更加高效的讀寫單個Parcelable對象的方法是:直接調用Parcelable.writeToParcel()和Parcelable.Creator.createFromParcel())

4 Bundles類

Bundles是一種型別安全的Map型容器,可用於儲存任何不同類型的資料。它具有很多對讀寫資料的效能最佳化,並且它的型別安全機制避免了當把它的資料封送到Parcel中時由於類型錯誤引起的BUG的調試的麻煩,可以使用的方法為: writeBundle(Bundle), readBundle(), and readBundle(ClassLoader)。

5 使用中的物件類

Parcel的一個非同尋常的特性是讀寫活對象的能力。對於使用中的物件,它們的內容實際上並沒有寫入,而是僅寫入了一個令牌來引用這個對象。當從Parcel中讀取這個對象時,你不會擷取一個新的對象執行個體,而是直接得到那個寫入的對象。有兩種使用中的物件可操作:

Binder對象。它是Android跨進程通訊的基礎。這種對象可被寫入Parcel,並在讀取時你將得到原始的對象或一個代理對象(可以想象:在進程內時得到原始的對象,在進程間時得到代理對象)。可以使用的方法們是: writeStrongBinder(IBinder), writeStrongInterface(IInterface), readStrongBinder(), writeBinderArray(IBinder[]), readBinderArray(IBinder[]), createBinderArray(), writeBinderList(List), readBinderList(List), createBinderArrayList()。

FileDescriptor對象。它代表了原始的Linux檔案描述符,它可以被寫入Parcel並在讀取時返回一個ParcelFileDescriptor對象用於操作原始的檔案描述符。ParcelFileDescriptor是原始描述符的一個複製:對象和fd不同,但是都操作於同一檔案流,使用同一個檔案位置指標,等等。可以使用的方法是:writeFileDescriptor(FileDescriptor), readFileDescriptor()。

6無類型容器類

    一類final方法,用於讀寫標準的java容器類。這些方法們是:writeArray(Object[]), readArray(ClassLoader), writeList(List), readList(List, ClassLoader), readArrayList(ClassLoader), writeMap(Map), readMap(Map, ClassLoader), writeSparseArray(SparseArray), readSparseArray(ClassLoader)。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.