import android.os.Parcel;
import android.os.Parcelable;
public class TearchServerInfo implements Parcelable{
public String IP;
public String PWD;
public String Email;
public String Name;
public int port;
public int id;
public void SetIP(String ip)
{
IP=ip;
}
public void SetPort(int port)
{
this.port = port;
}
public void SetID(int id)
{
this.id=id;
}
public void SetPWD(String pwd)
{
PWD = pwd;
}
public void SetEmail(String email)
{
Email = email;
}
public void SetName (String name)
{
Name = name;
}
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(IP);
dest.writeString(PWD);
dest.writeString(Email);
dest.writeString(Name);
dest.writeInt(port);
dest.writeInt(id);
}
public static final Parcelable.Creator<TearchServerInfo> CREATOR = new Parcelable.Creator<TearchServerInfo>()
{
public TearchServerInfo createFromParcel(Parcel in) {
TearchServerInfo TI = new TearchServerInfo();
TI.IP = in.readString();
TI.PWD = in.readString();
TI.Email = in.readString();
TI.Name= in.readString();
TI.port=in.readInt();
TI.id= in.readInt();
return TI;
}
public TearchServerInfo[] newArray(int size) {
return new TearchServerInfo[size];
}
};
}
上面的类我们现实了 Parcelable 的接口Parcel对象支持的写操作符,相应的读也差不多可以知道了!
· 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().
在数据包打包的时候即writeToParcel中与Parcelable.Creator解包的时候执行的顺序要一样!否则就会造成数据紊乱!
下面我们启动Intent 对象传递
TearchServerInfo ti = new new TearchServerInfo();
...
Intent IT = new Intent();
ComponentName cn = newComponentName("kaili.com", "kaili.com.Ansk");ComponentName 的好处就是不用在xml文件里面填activity
"kaili.com"这个为包名,"kaili.com.Ansk"这个是类名
IT.setComponent(cn);
IT.putExtra("std", ti);
startActivity(IT);
新的Activity起来之后我们就可以使用
TearchServerInfo ti =getIntent().getParcelableExtra("std");
把传递过来的对象获取过来了!