1. 程式人生 > 其它 >Android之Parcel

Android之Parcel

一、Parcel是什麼

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 general Parcelable interface), and references to live IBinder objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel.

  官方給出的解釋,Parcel是一個容器,可以包含資料或者物件引用,Parcel作為訊息的容器通過Binder傳送訊息,訊息包含資料和物件引用。Parcel支援序列化與IPC通訊後另一側進行反序列化。Parcel主要用來程序間IPC通訊的。

  在Andorid中,使用Binder進行IPC通訊,其資料載體就是Parcel。另外還有應用的Surface傳遞給SurfaceFlinger服務,Surface繼承自Parcelable。

二、原理

  Parcel是一套機制,可以將序列化後的資料寫入到共享記憶體中,其它程序通過Parcel將資料讀取,並反序列化成物件。