一、代码
1.xml
(1)activity_main.xml110 11 16 22 23
(2)AndroidManifest.xml
1 26 7 10 11 16 19 20 24 25 26 2721 22 23
2.java
(1)MainActivity.java1 package com.parcel; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.os.Parcel; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 10 public class MainActivity extends Activity {11 12 private Button getDataBtn,saveDataBtn;13 private Parcel parcel = null;14 @Override15 protected void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 setContentView(R.layout.activity_main);18 parcel = Parcel.obtain();19 saveDataBtn = (Button) findViewById(R.id.saveDataBtn);20 getDataBtn = (Button) findViewById(R.id.getDataBtn);21 saveDataBtn.setOnClickListener(new OnClickListener() {22 @Override23 public void onClick(View v) {24 parcel.writeString("testparcel");25 parcel.writeInt(666);26 parcel.writeFloat(0.999F);27 }28 });29 30 getDataBtn.setOnClickListener(new OnClickListener() {31 @Override32 public void onClick(View v) {33 parcel.setDataPosition(0);//把指针移到最前面34 String str = parcel.readString();35 int i = parcel.readInt();36 float f= parcel.readFloat();37 System.out.println(str);38 System.out.println(i);39 System.out.println(f);40 }41 });42 }43 }