1.创建Parcelable对象
/************************* *@author 徐宏明 E-Mail:android_xhm.126.com * QQ:294985925 *@version 创建时间 :2013-7-8 上午11:49:42 * *@see *************************/ package com.xhm.unit; import android.os.Parcel; import android.os.Parcelable; /** * Parcelable实现其可实现对象的序列化,并且能放入数组对象,比Serializable更高效。 * * @author Administrator * */ public class AddInfo implements Parcelable { private String mName, mNickname, mQq, mPhone, mEmail, mAddress;// 姓名,昵称,QQ,电话,邮箱,地址 public AddInfo(String name, String nickname, String qq, String phone, String email, String address) { // TODO Auto-generated constructor stub this.mName = name; this.mNickname = nickname; this.mQq = qq; this.mPhone = phone; this.mEmail = email; this.mAddress = address; } /** * 这个方法不晓得做什么的,返回0就可以。 */ @Override public int describeContents() { // TODO Auto-generated method stub return 0; } /** * 构造方法,用于读取对象信息,注意读写顺序要和写入顺序一致! * * @param in * 读入对象 */ private AddInfo(Parcel in) { mName = in.readString(); mNickname = in.readString(); mQq = in.readString(); mPhone = in.readString(); mEmail = in.readString(); mAddress = in.readString(); } /** * 将数据写入Parcel对象,写入的顺序和读取的顺序必须一致 */ @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeString(mName); dest.writeString(mNickname); dest.writeString(mQq); dest.writeString(mPhone); dest.writeString(mEmail); dest.writeString(mAddress); } /** * 固定方法,必须是static final 方法名必须大写 */ public static final Parcelable.Creator<AddInfo> CREATOR = new Parcelable.Creator<AddInfo>() { // 获得一个对象 public AddInfo createFromParcel(Parcel in) { return new AddInfo(in); } // 获得多个对象 public AddInfo[] newArray(int size) { return new AddInfo[size]; } }; public String getmName() { return mName; } public void setmName(String mName) { this.mName = mName; } public String getmNickname() { return mNickname; } public void setmNickname(String mNickname) { this.mNickname = mNickname; } public String getmQq() { return mQq; } public void setmQq(String mQq) { this.mQq = mQq; } public String getmPhone() { return mPhone; } public void setmPhone(String mPhone) { this.mPhone = mPhone; } public String getmEmail() { return mEmail; } public void setmEmail(String mEmail) { this.mEmail = mEmail; } public String getmAddress() { return mAddress; } public void setmAddress(String mAddress) { this.mAddress = mAddress; } }
2.使用Parcelable对象
// 初始化intent mBase_intent = new Intent(this, MySelf_Bitmap_Activity.class); // 初始化Parcelable对象 AddInfo info = new AddInfo(mView_myself_pop_edit[0].getText() .toString(), mView_myself_pop_edit[1].getText().toString(), mView_myself_pop_edit[2].getText().toString(), mView_myself_pop_edit[3].getText().toString(), mView_myself_pop_edit[4].getText().toString(), mView_myself_pop_edit[5].getText().toString()); // 添加Parcelable对象 mBase_intent.putExtra("myself_activity", info); // 跳转 startActivity(mBase_intent);
3.提取Parcelable对象中的信息
// 获得用户信息 mInfo = getIntent().getParcelableExtra("myself_activity_info");
4.Parcelable和Serializable性能对比: