android中intent放数据类型,Android中Intent传递的四种数据类型以及传递方法

1、Bundle

Bundle是将数据传递到另一个上下文中或保存,或者回复自己状态的数据存储方式,数据不是持久化状态。

(1)简单用法

//传递参数

Intent intentSimple = new Intent();

intentSimple.setClass(MainActivity.this,Activity2.class);

Bundle bundleSimple = new Bundle();

bundleSimple.putString("usr", "xcl");

bundleSimple.putString("pwd", "zj");

intentSimple.putExtras(bundleSimple);

startActivity(intentSimple);

//接收参数

Bundle bunde = this.getIntent().getExtras();

String eml = bunde.getString("usr");

String pwd = bunde.getString("pwd");

2、charsequence

主要用来传递String,char等

CharSequence接口,实现了这个接口的类有:CharBuffer、String、StringBuffer、StringBuilder这个四个类。 但是这个接口提供的方法有限,只有下面几个:charat、length、subSequence、toString这几个方法,感觉如果有必要,还是重载的比较好,避免用instaneof这个操作符。

3、Serializable

将对象序列化成二进制数据传递。

(1)简单用法

//传递参数

HashMap map2 = new HashMap();

map2.put("key1", "value1");

map2.put("key2", "value2");

Bundle bundleSerializable = new Bundle();

bundleSerializable.putSerializable("serializable", map2);

Intent intentSerializable = new Intent();

intentSerializable.putExtras(bundleSerializable);

intentSerializable.setClass(MainActivity.this,

SerializableActivity.class); startActivity(intentSerializable);

//接收参数

Bundle bundle = this.getIntent().getExtras(); ///传HashMap倒没有问题。

HashMap map =(HashMap)bundle.getSerializable("serializable");

String sResult = "map.size() ="+map.size();

Iterator iter = map.entrySet().iterator();

while(iter.hasNext()) {

Map.Entry entry = (Map.Entry)iter.next();

Object key = entry.getKey();

Object value = entry.getValue();

sResult +="\r\n key----> "+(String)key;

sResult +="\r\n value----> "+(String)value;

}

4、parcelable

这个android提供的一种新的类型,用来封装数据的容器,和Serializable相似,但是序列化的方式不同。

(1)实现parcelable接口

package com.chunsoft.fragmentbackstack;

import android.os.Parcel;

import android.os.Parcelable;

import java.util.HashMap;

/** * Developer:chunsoft on 2017/3/8 14:28 * Email:chun_soft@qq.com * Content: */

public class XclParcelable implements Parcelable{

//定义要传输的数据

public int mInt;

public String mStr;

public HashMap mMap = new HashMap();

protected XclParcelable(Parcel in) {

}

public Creator CREATOR = new Creator() {

@Override

public XclParcelable createFromParcel(Parcel in) {

//将映射在Parcel对象中的数据还原回来

//警告,这里顺序一定要和writeToParcel中定义的顺序一致才行!!!

mInt = in.readInt();

mStr = in.readString();

mMap = in.readHashMap(HashMap.class.getClassLoader());

return new XclParcelable(in);

}

@Override

public XclParcelable[] newArray(int size) {

return new XclParcelable[size];

}

};

@Override

public int describeContents() {

return 0;

}

//等于将数据映射到Parcel中去

@Override

public void writeToParcel(Parcel dest, int flags) {

dest.writeInt(mInt);

dest.writeString(mStr);

dest.writeMap(mMap);

}

}

(2)传递参数

Intent intentParcelable = new Intent();

XclParcelable xp = new XclParcelable();

xp.mInt = 1;

xp.mStr = "字符串";

xp.mMap = new HashMap();

xp.mMap.put("key", "value");

intentParcelable.putExtra("Parcelable", xp);

intentParcelable.setClass(MainActivity.this,

ParcelableActivity.class);

startActivity(intentParcelable);

(3)接收参数

Intent i = getIntent();

XclParcelable xp = i.getParcelableExtra("Parcelable");

(4)parcelable和Serializable的区别

Serializable的作用是保存对象的属性到本地文件,数据库,网络流等方便数据传输,也可程序之间传递。

parcelable的设计的目的是为了解决Serializable效率不高的问题,内存开销小,所以在内存间传递数据的方式用parcelable,缺点是不能持久化。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值