Parcel是一个轻量级的对象序列化,(类似Serierlizable), 可用于进程间通信,用的时候需要写个static CREATOR, 写入数据和读出的顺序必需一致。



这里是 一个Stackoverflow上的例子


研究了下怎么样传送List<T>, 下面这个例子直接传ArrayList<String>:

public class MyParcelInfo implements Parcelable {private int feildCount;private ArrayList<String> feildNameList;public MyParcelInfo(Parcel in) {feildCount = in.readInt();feildNameList = new ArrayList<String>();in.readList(feildNameList, ClassLoader.getSystemClassLoader());}public MyParcelInfo() {}public int describeContents() {return 0;}public void writeToParcel(Parcel dest, int flags) {dest.writeInt(feildCount);dest.writeList(feildNameList);}public static final Parcelable.Creator<MyParcelInfo> CREATOR = new Parcelable.Creator<MyParcelInfo>() {public MyParcelInfo createFromParcel(Parcel in) {return new MyParcelInfo(in);}public MyParcelInfo[] newArray(int size) {return new MyParcelInfo[size];}};public void setFeildCount(int count) {this.feildCount = count;}public int getFeildCount() {return this.feildCount;}public ArrayList<String> getFeildList() {return this.feildNameList;}public void setFeildList(ArrayList<String> list){this.feildNameList = list;}}下面是发送代码:protected void sendParcel() {
MyParcelInfo info = new MyParcelInfo();info.setFeildCount(4);ArrayList<String> list = new ArrayList<String>();list.add("Lisp");list.add("JNI");list.add("Net");list.add("Box2d");info.setFeildList(list);Intent it = new Intent(MainActivity.this, ParcelReceiverActivity.class);it.putExtra("info", info);this.startActivity(it);}

下面是接收代码:

MyParcelInfo info = this.getIntent().getParcelableExtra("info");ArrayList<String> infoList = info.getFeildList();



和String一样,如下的类型可以传送:
  • null
  • String
  • Byte
  • Short
  • Integer
  • Long
  • Float
  • Double
  • Boolean
  • String[]
  • boolean[]
  • byte[]
  • int[]
  • long[]
  • Object[] (supporting objects of the same type defined here).
  • Bundle
  • Map (as supported by writeMap).
  • Any object that implements the Parcelable protocol.
  • Parcelable[]
  • CharSequence (as supported by TextUtils.writeToParcel).
  • List (as supported by writeList).
  • SparseArray (as supported by writeSparseArray).
  • IBinder
  • Any object that implements Serializable (but see writeSerializable for caveats). Note that all of the previous types have relatively efficient implementations for writing to a Parcel; having to rely on the generic serialization approach is much less efficient and should be avoided whenever possible.
注意如果要传送其它自定义类型的对象,这个对象必须要implements Parcelable.

parcel可以用于Activity之间传送对象,相比于用Bundle传送,Parcel的好处是可以把数据打包成对象传送,可读性和可维性比用Bundle传一大堆Key,Value键值对要好,同时也可以扩展为传递其它类型的对象(implements Serializalbe or Parcelable).

关于进程间通信的应用,有待进一步研究。

   

更多相关文章

  1. Android(安卓)4.0 新增的显示数据集的桌面控件
  2. attrs.xml文件中属性类型format值的格式
  3. Android进程间通讯——AIDL
  4. Android(安卓)自动化测试(2)根据ID查找对象(java)
  5. android一个拨打电话的小程序
  6. Android中MimeType的用途
  7. Android(安卓)MimeType的用途以及所有类型
  8. android 创建自己的TabActivity
  9. 内容提供者的基本知识

随机推荐

  1. Android按下录音录音动画效果 ,自定义Vie
  2. android 9 第三方app默认拥有权限
  3. [Android]解决Android模拟器启动失败Fail
  4. Android:Toast重复显示的优化
  5. 徽章系列1: Top 30 android 开源项目徽章
  6. Android图片大小调整动态实现主要代码
  7. Error:Could not find com.android.tools
  8. Android(安卓)设置背景透明度
  9. popupWindow
  10. 第一行代码 第二版 前台服务 Android(安