你好,游客 登录 注册 搜索
背景:
阅读新闻

Android教程:Parcelable 序列化操作数据

[日期:2012-06-29] 来源:Linux社区  作者:andyhuabing [字体:大 中 小]
总结:
1、整个读写全是在内存中进行,主要是通过malloc()、realloc()、memcpy()等内存操作进行
2、读写时是PAD_SIZE=4字节对齐, #define PAD_SIZE(s) (((s)+3)&~3)
3、预分配的空间不够时newSize = ((mDataSize+len)*3)/2;会一次多分配50%
4、普通数据,使用的是mData内存地址,对于IBinder类型的数据以及FileDescriptor使用的是mObjects内存地址

下面举个小例子说明一下如何使用:
  1. package test.com.parcel;  
  2.   
  3. import Android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.os.Parcel;  
  7. import android.os.Parcelable;  
  8. import android.util.Log;  
  9.   
  10. public class TestParcelActivity extends Activity {  
  11.     ComplexDataStruct complexData = null;  
  12.           
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.           
  19.         testComplexDS();  
  20.     }  
  21.       
  22.     public void testComplexDS(){  
  23.         complexData = new ComplexDataStruct();  
  24.         Intent intent=new Intent(TestParcelActivity.this,TestParcelActivity.class);  
  25.   
  26.         complexData.a = 10;  
  27.         complexData.b = 20;  
  28.         complexData.str1 = " hello...";  
  29.         complexData.str2 = " world...";  
  30.         intent.putExtra("complexData", complexData);  
  31.           
  32.         ComplexDataStruct test = (ComplexDataStruct)intent.getParcelableExtra("complexData");  
  33.         Log.i("","a="+test.a);        
  34.         Log.i("","b="+test.b);  
  35.         Log.i("","str1="+test.str1);  
  36.         Log.i("","str2="+test.str2);  
  37.     }  
  38.       
  39.     /* 一个复杂的Parcelable对象传递 */  
  40.     public class ComplexDataStruct {  
  41.         public int a = 0;  
  42.         public int b = 0;  
  43.         public String str1 = null;  
  44.         public String str2 = null;  
  45.   
  46.         /* 内部实现静态CREATOR类 */  
  47.         public static final Parcelable.Creator<ComplexDataStruct> CREATOR =   
  48.             new Parcelable.Creator<ComplexDataStruct>() {  
  49.             // 从Parcel中读取数据,返回ComplexDataStruct对象   
  50.             public ComplexDataStruct createFromParcel(Parcel in) {  
  51.                 return new ComplexDataStruct(in);  
  52.             }  
  53.   
  54.             public ComplexDataStruct[] newArray(int size) {  
  55.                 return new ComplexDataStruct[size]; // 传递数组   
  56.             }  
  57.         };  
  58.           
  59.         ComplexDataStruct(){  
  60.             //TODO   
  61.         }  
  62.           
  63.         public ComplexDataStruct(ComplexDataStruct other) {  
  64.             this.a = other.a;  
  65.             this.b = other.b;  
  66.             this.str1 = other.str1;  
  67.             this.str2 = other.str2;  
  68.         }  
  69.           
  70.         private ComplexDataStruct(Parcel in) {  
  71.             readFromParcel(in);  
  72.         }  
  73.           
  74.         public void readFromParcel(Parcel in) {  
  75.             a = in.readInt();  
  76.             b = in.readInt();  
  77.             str1 = in.readString();  
  78.             str2 = in.readString();  
  79.         }  
  80.   
  81.         public int describeContents() {  
  82.             // TODO Auto-generated method stub   
  83.             return 0;  
  84.         }  
  85.   
  86.         public void writeToParcel(Parcel arg0, int arg1) {  
  87.             // TODO Auto-generated method stub   
  88.             arg0.writeInt(a);  
  89.             arg0.writeInt(b);  
  90.             arg0.writeString(str1);  
  91.             arg0.writeString(str2);  
  92.         }  
  93.     }  
  94. }  
linux
相关资讯       Android开发基础 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款