Android Intent 使用 Parcel 反序列化出错.


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
null
null
null
Bundle[mParcelledData.dataSize=0]
0
null
null
null
null


context.startActivity(readIntent);// Activite 无法启动, 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内存如下:

[22, 0, 0, 0, 99, 0, 110, 0, 46, 0, 101, 0, 98, 0, 101, 0, 110, 0, 46, 0, 98, 0, 111, 0, 111, 0, 107, 0, 115, 0, 104, 0, 101, 0, 108, 0, 102, 0, 46, 0, 86, 0, 73, 0, 69, 0, 87, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 16, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1]


cv.put("intent", data); //将parcel对持久化.


///读取


byte[] data = cursor.getBlob(cursor.getColumnIndex("intent"));

//查看读取到的 Data内存如下: 与写入的相同.

[22, 0, 0, 0, 99, 0, 110, 0, 46, 0, 101, 0, 98, 0, 101, 0, 110, 0, 46, 0, 98, 0, 111, 0, 111, 0, 107, 0, 115, 0, 104, 0, 101, 0, 108, 0, 102, 0, 46, 0, 86, 0, 73, 0, 69, 0, 87, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 16, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1]


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 反序列化失败.

Intent { (has extras) }
null
null
null
null
null
Bundle[mParcelledData.dataSize=0]
0
null
null
null
null
}



查看Android Intnet 源代码:

看不出来有什么异常

    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 static final Parcelable.Creator<Intent> CREATOR            = new Parcelable.Creator<Intent>() {        public Intent createFromParcel(Parcel in) {            return new Intent(in);        }        public Intent[] newArray(int size) {            return new Intent[size];        }    };    /** @hide */    protected Intent(Parcel in) {        readFromParcel(in);    }    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<String>();            int i;            for (i=0; i<N; i++) {                mCategories.add(in.readString().intern());            }        } else {            mCategories = null;        }        if (in.readInt() != 0) {            mSelector = new Intent(in);        }        if (in.readInt() != 0) {            mClipData = new ClipData(in);        }        mExtras = in.readBundle();    } 



更多相关文章

  1. Android(安卓)4.4 Kitkat Phone工作流程浅析(十二)__4.4小结与5.
  2. ADB server didn't ACK
  3. Android(安卓)4.0中状态栏上各个按钮的点击实现
  4. Android-Activity的四状态、七生命周期、和四启动模式
  5. Android(安卓)中文 API (29) —— CompoundButton
  6. android setSelected及view相关知识
  7. android插入耳机状态使用扬声器外放音乐
  8. Android:requestWindowFeature应用程序窗体显示状态操作
  9. 关于android 内存的笔记

随机推荐

  1. Android的包管理机制浅析(一)
  2. Android中设置控件可见与不可见详…
  3. Flutter 基础(二)基本组件ListView
  4. android 浮层简单实现、activity设置Them
  5. Android Studio 运行项目报错:org.objectw
  6. Android属性汇总
  7. Android(安卓)webView自适应屏幕
  8. Android关于绘图中Shader 的效果(中级)
  9. FrameLayout 帧布局
  10. 2017-9-16(沉浸式状态栏StatusBar)