編輯:關於 Android 程式設計
Android 的 Parcel 可以序列化與反序列化資料,但上層的 Intent 卻出現了反序列化失敗的情況。翻閱 Android 原始碼中 Intent 的 Parcel 介面程式碼,也未能直接找出問題所在。
以下是我的測試程式碼。
final Intent intent = new Intent("cn.eben.bookshelf.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 序列化前 Intent 內存:Intent { act=cn.eben.bookshelf.VIEW flg=0x10000000 }
Parcel parcel = Parcel.obtain();
intent.writeToParcel(parcel, 0);
byte[] data = parcel.marshall();
Parcel readParcel = Parcel.obtain();
readParcel.unmarshall(data, 0, data.length);
Intent readIntent = Intent.CREATOR.createFromParcel(readParcel);
// readIntent 內存狀態,Intent 反序列化失敗:
// Intent { (has extras) } / null / null ... Bundle[mParcelledData.dataSize=0] ...
context.startActivity(readIntent); // Activity 無法啟動,Intent 無效
final Intent intent = new Intent("cn.eben.bookshelf.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 序列化前 Intent 內存:Intent { act=cn.eben.bookshelf.VIEW flg=0x10000000 }
Parcel parcel = Parcel.obtain();
intent.writeToParcel(parcel, 0);
byte[] data = parcel.marshall();
// data 內容為一串包含 action 字串與 flags 的位元組陣列
cv.put("intent", data); // 將 parcel 對持久化
// 讀取
byte[] data = cursor.getBlob(cursor.getColumnIndex("intent"));
// 讀取到的 data 與寫入的完全相同
if (null != data && 1 < data.length) {
Intent intent = null;
Parcel parcel = Parcel.obtain();
parcel.unmarshall(data, 0, data.length);
intent = Intent.CREATOR.createFromParcel(parcel);
// intent 內存狀態,Intent 反序列化失敗
}
查看 Android Intent 原始碼,writeToParcel 與 readFromParcel 的邏輯看似並無異常:
public void writeToParcel(Parcel out, int flags) {
out.writeString(mAction);
Uri.writeToParcel(out, mData);
out.writeString(mType);
out.writeInt(mFlags);
out.writeString(mPackage);
ComponentName.writeToParcel(mComponent, out);
if (mSourceBounds != null) {
out.writeInt(1);
mSourceBounds.writeToParcel(out, flags);
} else {
out.writeInt(0);
}
if (mCategories != null) {
out.writeInt(mCategories.size());
for (String category : mCategories) {
out.writeString(category);
}
} else {
out.writeInt(0);
}
if (mSelector != null) {
out.writeInt(1);
mSelector.writeToParcel(out, flags);
} else {
out.writeInt(0);
}
if (mClipData != null) {
out.writeInt(1);
mClipData.writeToParcel(out, flags);
} else {
out.writeInt(0);
}
out.writeBundle(mExtras);
}
public void readFromParcel(Parcel in) {
setAction(in.readString());
mData = Uri.CREATOR.createFromParcel(in);
mType = in.readString();
mFlags = in.readInt();
mPackage = in.readString();
mComponent = ComponentName.readFromParcel(in);
if (in.readInt() != 0) {
mSourceBounds = Rect.CREATOR.createFromParcel(in);
}
int N = in.readInt();
if (N > 0) {
mCategories = new HashSet();
for (int i=0; i
仔細對照序列化與反序列化的讀寫順序,是排查這類問題的關鍵所在。建議重點檢查 marshall 與 unmarshall 過程中的資料對齊,以及自定義 Parcelable 物件的讀寫順序是否嚴格一致。
由淺入深徹底搞懂 Android AIDL 與 Binder 機制
一、Binder 機制概覽 1.1 為什麼 Android 選擇 Binder 出於安全性、穩定性以及記憶體管理的多重考量,Android 將應用程式與系統服務分別運行在
Android TextView 中文數字混排不對齊?自定義 AlignTextView 解決
用 TextView 顯示中文與數字混雜的文字時,常常會出現一個惱人的現象:文字還沒到螢幕右邊就換行了,右側留出一塊空白,左邊卻擠成一團,整體視覺相當凌亂。 這是因
用反射呼叫系統隱藏遠程服務,實作來電攔截
早期版本的 Android 在 TelephonyManager 上開放了一個 endCall() 方法,掛斷來電一行程式碼就能搞定。但 Google 後來意識到,對一支
Android 5.0 新增能力:用 Palette 為界面自動配色
Palette 既好用又好玩。它的作用是從圖像中提取突出的顏色,我們可以把提取到的色值賦給 Toolbar、標題、狀態欄等,讓整個界面的色調保持統一,視覺效果非常出色。