一:
转自: http://blog.sina.com.cn/s/blog_78e3ae430100pxba.html
android提供了一种新的类型:Parcel。本类被用作封装数据的容器,封装后的数据可以通过Intent或IPC传递。 除了基本类型以
外,只有实现了Parcelable接口的类才能被放入Parcel中。
Parcelable实现要点:需要实现三个东西
1)writeToParcel 方法。该方法将类的数据写入外部提供的Parcel中.声明如下:
writeToParcel (Parcel dest, int flags) 具体参数含义见javadoc
2)describeContents方法。没搞懂有什么用,反正直接返回0也可以
3)静态的Parcelable.Creator接口,本接口有两个方法:
createFromParcel(Parcel in) 实现从in中创建出类的实例的功能
newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话(return new T[size])即可。估计本方法是供外部类反序列化本类数组使用。
测试用的接收信息Activity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
public class Test extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = getIntent();
Person p = i.getParcelableExtra("yes");
System.out.println("---->"+p.name);
System.out.println("---->"+p.map.size());
}
}
发送的Activity
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class TestNew extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
Person p = new Person();
p.map = new HashMap<String,String>();
p.map.put("yes", "ido");
p.name="ok";
intent.putExtra("yes", p);
intent.setClass(this, Test.class);
startActivity(intent);
}
}
Parcelable的实现类
import java.util.HashMap;
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable {
public HashMap<String,String> map = new HashMap<String,String> ();
public String name ;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeMap(map);
dest.writeString(name);
}
public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
//重写Creator
@Override
public Person createFromParcel(Parcel source) {
Person p = new Person();
p.map=source.readHashMap(HashMap.class.getClassLoader());
p.name=source.readString();
return p;
}
@Override
public Person[] newArray(int size) {
// TODO Auto-generated method stub
return null;
}
};
}
二:
转自:http://ipjmc.iteye.com/blog/1314145
Android序列化对象主要有两种方法,实现Serializable接口、或者实现Parcelable接口。实现Serializable接口是Java SE本身就支持的,而Parcelable是Android特有的功能,效率比实现Serializable接口高,而且还可以用在IPC中。实现Serializable接口非常简单,声明一下就可以了,而实现Parcelable接口稍微复杂一些,但效率更高,推荐用这种方法提高性能。下面就介绍一下实现Parcelable接口的方法
通过实现Parcelable接口序列化对象的步骤:
1、声明实现接口Parcelable
2、实现Parcelable的方法writeToParcel,将你的对象序列化为一个Parcel对象
3、实例化静态内部对象CREATOR实现接口Parcelable.Creator:
- public static final Parcelable.Creator<T> CREATOR
public static final Parcelable.Creator<T> CREATOR
其中public static final一个都不能少,内部对象CREATOR的名称也不能改变,必须全部大写。
4、完成CREATOR的代码,实现方法createFromParcel,将Parcel对象反序列化为你的对象
简而言之:通过writeToParcel将你的对象映射成Parcel对象,再通过createFromParcel将Parcel对象映射成你的对象。也可以将Parcel看成是一个流,通过writeToParcel把对象写到流里面,在通过createFromParcel从流里读取对象,只不过这个过程需要你来实现,因此写的顺序和读的顺序必须一致。
Parcel对象可以通过以下方法写入或读取byte, double, float, int, long, String这6种类型变量。
- writeByte(byte), readByte()
- writeDouble(double), readDouble()
- writeFloat(float), readFloat()
- writeInt(int), readInt()
- writeLong(long), readLong()
- writeString(String), readString()
writeByte(byte), readByte()
writeDouble(double), readDouble()
writeFloat(float), readFloat()
writeInt(int), readInt()
writeLong(long), readLong()
writeString(String), readString()
也可以读取或写入他们的数组
- writeBooleanArray(boolean[]), readBooleanArray(boolean[]), createBooleanArray()
- writeByteArray(byte[]), writeByteArray(byte[], int, int), readByteArray(byte[]), createByteArray()
- writeCharArray(char[]), readCharArray(char[]), createCharArray()
- writeDoubleArray(double[]), readDoubleArray(double[]), createDoubleArray()
- writeFloatArray(float[]), readFloatArray(float[]), createFloatArray()
- writeIntArray(int[]), readIntArray(int[]), createIntArray()
- writeLongArray(long[]), readLongArray(long[]), createLongArray()
- writeStringArray(String[]), readStringArray(String[]), createStringArray()
- writeSparseBooleanArray(SparseBooleanArray), readSparseBooleanArray()
writeBooleanArray(boolean[]), readBooleanArray(boolean[]), createBooleanArray()
writeByteArray(byte[]), writeByteArray(byte[], int, int), readByteArray(byte[]), createByteArray()
writeCharArray(char[]), readCharArray(char[]), createCharArray()
writeDoubleArray(double[]), readDoubleArray(double[]), createDoubleArray()
writeFloatArray(float[]), readFloatArray(float[]), createFloatArray()
writeIntArray(int[]), readIntArray(int[]), createIntArray()
writeLongArray(long[]), readLongArray(long[]), createLongArray()
writeStringArray(String[]), readStringArray(String[]), createStringArray()
writeSparseBooleanArray(SparseBooleanArray), readSparseBooleanArray()
更多操作可以参阅:
http://developer.android.com/reference/android/os/Parcel.html
示例代码:
- package com.ipjmc.demo.parcelable;
-
-
- import java.util.Date;
-
- import android.os.Parcel;
- import android.os.Parcelable;
-
- public class ParcelableDate implements Parcelable {
-
-
- public long mId;
- public Date mDate;
-
- public ParcelableDate(long id, long time) {
- mId = id;
- mDate = new Date(time);
- }
-
- public ParcelableDate(Parcel source) {
-
- mId = source.readLong();
- mDate = new Date(source.readLong());
- }
-
- @Override
- public int describeContents() {
- return 0;
- }
-
-
- @Override
- public void writeToParcel(Parcel dest, int flags) {
-
- dest.writeLong(mId);
- dest.writeLong(mDate.getTime());
- }
-
-
- public static final Parcelable.Creator<ParcelableDate> CREATOR = new Creator<ParcelableDate>() {
-
- @Override
- public ParcelableDate[] newArray(int size) {
- return new ParcelableDate[size];
- }
-
-
- @Override
- public ParcelableDate createFromParcel(Parcel source) {
- return new ParcelableDate(source);
- }
- };
- }