
相信通过前面的介绍,你一定很了解了了Parcel的存储机制,下面给定一应用程序来实践。
1、布局文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="@string/hello" />
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent" android:layout_height="wrap_content">
- <Button android:id="@+id/btWriteByte" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="写入一个byte值"></Button>
- <Button android:id="@+id/btWriteInt" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="写入一个int值"></Button>
- </LinearLayout>
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent" android:layout_height="wrap_content">
- <Button android:id="@+id/btWriteDouble" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="写入一个double值"></Button>
- <Button android:id="@+id/btWriteString" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="写入一个String值"></Button>
- </LinearLayout>
- <View android:layout_width="fill_parent" android:layout_height="2dip"
- android:background="#FF1493"></View>
- <LinearLayout android:orientation="horizontal"
- android:layout_marginTop="5dip" android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Button android:id="@+id/btReadByte" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="读取一个byte值"></Button>
- <Button android:id="@+id/btReadInt" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="读取一个int值"></Button>
- </LinearLayout>
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent" android:layout_height="wrap_content">
- <Button android:id="@+id/btReadDouble" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="读取一个double值"></Button>
- <Button android:id="@+id/btReadString" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="读取一个String值"></Button>
- </LinearLayout>
- <View android:layout_width="fill_parent" android:layout_height="2dip"
- android:background="#FF1493"></View>
- <Button android:id="@+id/btSameType" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="利用setDataPosition读取多个值"></Button>
- </LinearLayout>
2、配置文件如下:
[html]
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.qinjuning.parcel"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
3、程序主文件如下:
- public class MainActivity extends Activity implements OnClickListener {
- private static String TAG = "PARCELTEST";
- // Button ID
- private static int[] btIds = new int[] { R.id.btWriteByte, R.id.btWriteInt,
- R.id.btReadDouble, R.id.btWriteString, R.id.btReadByte,
- R.id.btReadInt, R.id.btReadDouble, R.id.btReadString,
- R.id.btSameType };
- // 每种类型的当前值
- private byte cur_byte = 1; // 每次总写入 false
- private int cur_int = 10; // 写入值 cur_int ++ ;
- private double cur_float = 100.0d; // 写入值 cur_float++ ;
- private String cur_str = "QinJun -->" + cur_int; // 写入值 "QinJun -->"+cur_int
- private Parcel parcel = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- for (int i = 0; i < btIds.length; i++) {
- Button bt = (Button) findViewById(btIds[i]);
- bt.setOnClickListener(this);
- }
- parcel = Parcel.obtain(); // 获得一个Parcel对象 ,相当于new一个,初始大小为0
- Log.i(TAG, "The original parcel info" + getParcelInfo());
- }
- @Override
- public void onClick(View view) {
- // TODO Auto-generated method stub
- int viewviewId = view.getId();
- switch (viewId) {
- case R.id.btWriteByte:
- parcel.setDataPosition(0);
- parcel.writeByte(cur_byte);
- Log.i(TAG, " after write byte, --->" + getParcelInfo());
- break;
- case R.id.btWriteInt:
- parcel.writeInt(cur_int);
- Log.i(TAG, " after write int, --->" + getParcelInfo());
- break;
- case R.id.btWriteDouble:
- parcel.writeDouble(cur_float);
- Log.i(TAG, " after write float, --->" + getParcelInfo());
- break;
- case R.id.btWriteString:
- parcel.writeString(cur_str);
- Log.i(TAG, " after write String, --->" + getParcelInfo());
- break;
- case R.id.btReadByte:
- byte b = parcel.readByte();
- Log.i(TAG, " read byte is=" + b + ", --->" + getParcelInfo()
- + "String");
- break;
- case R.id.btReadInt:
- int i = parcel.readInt();
- Log.i(TAG, " read int is=" + i + ", --->" + getParcelInfo());
- break;
- case R.id.btReadDouble:
- float f = parcel.readFloat();
- readSameType();
- Log.i(TAG, " read float is=" + f + ", --->" + getParcelInfo());
- break;
- case R.id.btReadString:
- parcel.setDataPosition(0);
- String str = parcel.readString();
- Log.i(TAG, " read float is=" + str + ", --->" + getParcelInfo());
- break;
- case R.id.btSameType:
- readSameType();
- break;
- default:
- break;
- }
- }
- private String getParcelInfo() {// 得到parcel的信息
- return "dataSize = " + parcel.dataSize() + ", dataCapacity="
- + parcel.dataCapacity() + ", dataPositon = "
- + parcel.dataPosition();
- }
- /**
- * 前提条件,Parcel存在多个类型相同的对象,本例子以10个float对象说明:
- */
- public void readSameType() {
- for (int i = 0; i < 10; i++) {
- parcel.writeDouble(i);
- Log.i(TAG, "write double ----> " + getParcelInfo());
- }
- //方法一 ,显示设置偏移量
- int i = 0;
- int datasize = parcel.dataSize();
- while (i < datasize) {
- parcel.setDataPosition(i);
- double fvalue = parcel.readDouble();
- Log.i(TAG, " read double is=" + fvalue + ", --->" + getParcelInfo());
- i += 8; // double占用字节为 8byte
- }
- // 方法二,由于对象的类型一致,我们可以直接利用readXXX()读取值会产生偏移量
- // parcel.setDataPosition(0) ; //
- // while(parcel.dataPosition()<parcel.dataSize()){
- // double fvalue = parcel.readDouble();
- // Log.i(TAG, " read double is=" + fvalue + ", --->" + getParcelInfo());
- // }
- }
- }
由于取值时,可能存在类型的转换,因此点击按钮时,可能不会产生预期结果。因此,得保证偏移量对应数值的正确性。
二、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
