Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Intent 使用 Parcel 反序列化出錯問題排查

編輯:關於 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 物件的讀寫順序是否嚴格一致。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved