Android Intent 之间传递对象之 Parcelable接口

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");

把传递过来的对象获取过来了!


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值