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

Android中Parcel的分析以及使用

[日期:2012-02-02] 来源:Linux社区  作者:qinjuning [字体:大 中 小]
下面,给出一张浅显的Parcel的存放空间图,希望大家在理解的同时,更能体味其中滋味。有点简单,求谅解。

               

相信通过前面的介绍,你一定很了解了了Parcel的存储机制,下面给定一应用程序来实践。

     1、布局文件如下:

[html]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <TextView android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content" android:text="@string/hello" />  
  7.     <LinearLayout android:orientation="horizontal"  
  8.         android:layout_width="fill_parent" android:layout_height="wrap_content">  
  9.         <Button android:id="@+id/btWriteByte" android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content" android:text="写入一个byte值"></Button>  
  11.         <Button android:id="@+id/btWriteInt" android:layout_width="wrap_content"  
  12.             android:layout_height="wrap_content" android:text="写入一个int值"></Button>  
  13.     </LinearLayout>  
  14.     <LinearLayout android:orientation="horizontal"  
  15.         android:layout_width="fill_parent" android:layout_height="wrap_content">  
  16.         <Button android:id="@+id/btWriteDouble" android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content" android:text="写入一个double值"></Button>  
  18.         <Button android:id="@+id/btWriteString" android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content" android:text="写入一个String值"></Button>  
  20.     </LinearLayout>  
  21.     <View android:layout_width="fill_parent" android:layout_height="2dip"  
  22.         android:background="#FF1493"></View>  
  23.     <LinearLayout android:orientation="horizontal"  
  24.         android:layout_marginTop="5dip" android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content">  
  26.         <Button android:id="@+id/btReadByte" android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content" android:text="读取一个byte值"></Button>  
  28.         <Button android:id="@+id/btReadInt" android:layout_width="wrap_content"  
  29.             android:layout_height="wrap_content" android:text="读取一个int值"></Button>  
  30.     </LinearLayout>  
  31.     <LinearLayout android:orientation="horizontal"  
  32.         android:layout_width="fill_parent" android:layout_height="wrap_content">  
  33.         <Button android:id="@+id/btReadDouble" android:layout_width="wrap_content"  
  34.             android:layout_height="wrap_content" android:text="读取一个double值"></Button>  
  35.         <Button android:id="@+id/btReadString" android:layout_width="wrap_content"  
  36.             android:layout_height="wrap_content" android:text="读取一个String值"></Button>  
  37.     </LinearLayout>  
  38.     <View android:layout_width="fill_parent" android:layout_height="2dip"  
  39.         android:background="#FF1493"></View>  
  40.     <Button android:id="@+id/btSameType" android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content" android:text="利用setDataPosition读取多个值"></Button>  
  42. </LinearLayout>   

 2、配置文件如下:

[html]

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.qinjuning.parcel"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".MainActivity"  android:label="@string/app_name">  
  8.             <intent-filter>  
  9.                 <action android:name="android.intent.action.MAIN" />  
  10.                 <category android:name="android.intent.category.LAUNCHER" />  
  11.             </intent-filter>  
  12.         </activity>  
  13.     </application>  
  14. </manifest>   

     3、程序主文件如下:

[html]
  1. public class MainActivity extends Activity implements OnClickListener {  
  2.   
  3.     private static String TAG = "PARCELTEST";  
  4.     // Button ID  
  5.     private static int[] btIds = new int[] { R.id.btWriteByte, R.id.btWriteInt,  
  6.             R.id.btReadDouble, R.id.btWriteString, R.id.btReadByte,  
  7.             R.id.btReadInt, R.id.btReadDouble, R.id.btReadString,  
  8.             R.id.btSameType };  
  9.     // 每种类型的当前值  
  10.     private byte cur_byte = 1; // 每次总写入 false  
  11.     private int cur_int = 10; // 写入值 cur_int ++ ;  
  12.     private double cur_float = 100.0d; // 写入值 cur_float++ ;  
  13.     private String cur_str = "QinJun -->" + cur_int; // 写入值 "QinJun -->"+cur_int  
  14.   
  15.     private Parcel parcel = null;  
  16.   
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.         for (int i = 0; i < btIds.length; i++) {  
  22.             Button bt = (Button) findViewById(btIds[i]);  
  23.             bt.setOnClickListener(this);  
  24.         }  
  25.         parcel = Parcel.obtain(); // 获得一个Parcel对象 ,相当于new一个,初始大小为0  
  26.         Log.i(TAG, "The original parcel info" + getParcelInfo());  
  27.     }  
  28.   
  29.     @Override  
  30.     public void onClick(View view) {  
  31.         // TODO Auto-generated method stub  
  32.         int viewviewId = view.getId();  
  33.         switch (viewId) {  
  34.         case R.id.btWriteByte:  
  35.             parcel.setDataPosition(0);  
  36.             parcel.writeByte(cur_byte);  
  37.             Log.i(TAG, " after write byte, --->" + getParcelInfo());  
  38.             break;  
  39.         case R.id.btWriteInt:  
  40.             parcel.writeInt(cur_int);  
  41.             Log.i(TAG, " after write int, --->" + getParcelInfo());  
  42.             break;  
  43.         case R.id.btWriteDouble:  
  44.             parcel.writeDouble(cur_float);  
  45.             Log.i(TAG, " after write float, --->" + getParcelInfo());  
  46.             break;  
  47.         case R.id.btWriteString:  
  48.             parcel.writeString(cur_str);  
  49.             Log.i(TAG, " after write String, --->" + getParcelInfo());  
  50.             break;  
  51.         case R.id.btReadByte:  
  52.             byte b = parcel.readByte();  
  53.             Log.i(TAG, " read byte is=" + b + ", --->" + getParcelInfo()  
  54.                     + "String");  
  55.             break;  
  56.         case R.id.btReadInt:  
  57.             int i = parcel.readInt();  
  58.             Log.i(TAG, " read int is=" + i + ", --->" + getParcelInfo());  
  59.             break;  
  60.         case R.id.btReadDouble:  
  61.             float f = parcel.readFloat();  
  62.             readSameType();  
  63.             Log.i(TAG, " read float is=" + f + ", --->" + getParcelInfo());  
  64.             break;  
  65.         case R.id.btReadString:  
  66.             parcel.setDataPosition(0);  
  67.             String str = parcel.readString();  
  68.             Log.i(TAG, " read float is=" + str + ", --->" + getParcelInfo());  
  69.             break;  
  70.         case R.id.btSameType:  
  71.             readSameType();  
  72.             break;  
  73.         default:  
  74.             break;  
  75.         }  
  76.     }  
  77.   
  78.     private String getParcelInfo() {// 得到parcel的信息  
  79.         return "dataSize = " + parcel.dataSize() + ", dataCapacity="  
  80.                 + parcel.dataCapacity() + ", dataPositon = "  
  81.                 + parcel.dataPosition();  
  82.     }  
  83.   
  84.     /**  
  85.      * 前提条件,Parcel存在多个类型相同的对象,本例子以10个float对象说明:  
  86.      */  
  87.     public void readSameType() {  
  88.           
  89.         for (int i = 0; i < 10; i++) {  
  90.             parcel.writeDouble(i);  
  91.             Log.i(TAG, "write double ----> " + getParcelInfo());  
  92.         }  
  93.         //方法一 ,显示设置偏移量   
  94.         int i = 0;  
  95.         int datasize = parcel.dataSize();  
  96.         while (i < datasize) {  
  97.             parcel.setDataPosition(i);  
  98.             double fvalue = parcel.readDouble();  
  99.             Log.i(TAG, " read double is=" + fvalue + ", --->" + getParcelInfo());  
  100.             i += 8; // double占用字节为 8byte   
  101.         }  
  102. //      方法二,由于对象的类型一致,我们可以直接利用readXXX()读取值会产生偏移量  
  103. //      parcel.setDataPosition(0)  ;  //  
  104. //      while(parcel.dataPosition()<parcel.dataSize()){  
  105. //          double fvalue = parcel.readDouble();  
  106. //          Log.i(TAG, " read double is=" + fvalue + ", --->" + getParcelInfo());  
  107. //      }  
  108.     }  
  109. }  
  110.    

由于取值时,可能存在类型的转换,因此点击按钮时,可能不会产生预期结果。因此,得保证偏移量对应数值的正确性。


二、Parcel的使用 

关于Parcel的使用,请参考如下两票文章,小生也就不再赘述了。

Android中Intent传递对象的两种方法(Serializable,Parcelable)! http://www.linuxidc.com/Linux/2012-02/53067.htm

Android 中的AIDL!!!   http://www.linuxidc.com/Linux/2012-02/53068.htm

linux
相关资讯       Android开发教程 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2012/10/11 14:47:12
放这里面有什么用处呢??