轉載千里草的文章,感謝其無私奉獻!原文地址
Seriaizeable 可以序列化長期保存到本地文件裏,那麼Parcelable 可不可以呢?
在閱讀了Parcel的源碼之後,發現了很多的 writexxx 和 readxxx 方法,這個後續再做分析,這篇主要描述一下另外兩個方法marshall 和 unmarshall, 這兩個函數是相互對應的.
marshall: Parcel 將自身所保存的所有數據以 byte 數組的形式返回
/**
* Returns the raw bytes of the parcel.
*
* <p class="note">The data you retrieve here <strong>must not</strong>
* be placed in any kind of persistent storage (on local disk, across
* a network, etc). For that, you should use standard serialization
* or another kind of general serialization mechanism. The Parcel
* marshalled representation is highly optimized for local IPC, and as
* such does not attempt to maintain compatibility with data created
* in different versions of the platform.
*/
public final byte[] marshall() {
return nativeMarshall(mNativePtr);
}
unmarshall: 從 byte 數組裏獲取數據,存入自身 Parcel
/**
* Set the bytes in data to be the raw bytes of this Parcel.
*/
public final void unmarshall(byte[] data, int offset, int length) {
nativeUnmarshall(mNativePtr, data, offset, length);
}
既然Parcel 可以轉化成 byte[],也可以從byte[] 裏獲取數據,那麼我們就可以這樣做:
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
public class ParcelableUtil {
public static byte[] marshall(Parcelable parceable) {
Parcel parcel = Parcel.obtain();
parcel.setDataPosition(0);
parceable.writeToParcel(parcel, 0);
byte[] bytes = parcel.marshall();
Log.d("ParcelableTest", "bytes = " + String.valueOf(bytes) + "parcel" + parcel.toString());
parcel.recycle();
return bytes;
}
public static Parcel unmarshall(byte[] bytes) {
Parcel parcel = Parcel.obtain();
parcel.unmarshall(bytes, 0, bytes.length);
parcel.setDataPosition(0);
return parcel;
}
}
// MainActivity.java
private void testParcelableMarshall() {
ParcelableTest parcelableTest = new ParcelableTest(1, 2, 3, "HelloWorld!");
parcelableTest.toString();
byte[] bytes = ParcelableUtil.marshall(parcelableTest);
SharedPreferences.Editor parcelableEditor = getSharedPreferences("parcelable", 0).edit();
//parcelableEditor.putString("parcelable", new String(bytes));
parcelableEditor.putString("parcelable", Base64.encodeToString(bytes, 0));
parcelableEditor.commit();
SharedPreferences parcelable = getSharedPreferences("parcelable", 0);
//Parcel parcel = ParcelableUtil.unmarshall(parcelable.getString("parcelable", "default").getBytes());
Parcel parcel = ParcelableUtil.unmarshall(Base64.decode(parcelable.getString("parcelable", "default"), 0));
Log.d("ParcelableTest", String.valueOf(bytes).toString() + "bytes = " + parcelable.getString("parcelable", "default").toString() + "parcel" + parcel.toString());
ParcelableTest parcelableTest1 = null;
parcelableTest1 = ParcelableTest.CREATOR.createFromParcel(parcel);
parcelableTest1.toString();
}
在調用 testParcelableMarshall() 之後結果如下:
D/ParcelableTest( 7923): t1 = 1 t2 = 2 t3 = 3 a = HelloWorld!
D/ParcelableTest( 7923): Call writeToParcel
D/ParcelableTest( 7923): bytes = [B@3043c569parcelandroid.os.Parcel@b373bee
D/ParcelableTest( 7923): [B@3043c569bytes = AQAAAAIAAAADAAAACwAAAEgAZQBsAGwAbwBXAG8AcgBsAGQAIQAAAA==
D/ParcelableTest( 7923): parcelandroid.os.Parcel@b373bee
D/ParcelableTest( 7923): Call createFromParcel
D/ParcelableTest( 7923): Call ParcelableTest
D/ParcelableTest( 7923): t1 = 1 t2 = 2 t3 = 3 a = HelloWorld!
事實證明,Parcelable 成功的保存到了持久化的文件裏。
如圖所示: 如圖所示