不同进程之间交换数据通过Parcelable包装交换数据.可以通过Intent在不同的进程之间传送数据.
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的数组,仅一句话(returnnew T[size])即可。估计本方法是供外部类反序列化本类数组使用。
测试:
package com.chen.parcel;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Test extends Activity {
/** Called when the activity is first created. */
@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("1", "shi");
p.map.put("2", "jun");
p.name="chen";
intent.putExtra("yes", p);
intent.setClass(this, TestNew.class);
startActivity(intent);
}
}
package com.chen.parcel;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class TestNew extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = getIntent();
Person p = i.getParcelableExtra("yes");
Log.i("chen", p.name);
Log.i("chen", p.map.get("1")+"");
Log.i("chen", p.map.get("2")+"");
}
}
package com.chen.parcel;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.SortedMap;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Pack200.Packer;
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable, Packer {
public HashMap<String,String> map = new HashMap<String,String> ();
public String name ;
public int describeContents() {
return 0;
}
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
public Person createFromParcel(Parcel source) {
Person p = new Person();
p.map=source.readHashMap(HashMap.class.getClassLoader());
p.name=source.readString();
return p;
}
public Person[] newArray(int size) {
// TODO Auto-generated method stub
return null;
}
};
public SortedMap<String, String> properties() {
// TODO Auto-generated method stub
return null;
}
public void pack(JarFile in, OutputStream out) throws IOException {
// TODO Auto-generated method stub
}
public void pack(JarInputStream in, OutputStream out)
throws IOException {
// TODO Auto-generated method stub
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
// TODO Auto-generated method stub
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
// TODO Auto-generated method stub
}
}