android Bundle在同类中与Intent中传送数据、复杂的对象

本文详细介绍在Android应用中如何使用Bundle和Intent进行数据传递,包括简单类型数据和复杂对象的封装与解析过程,为开发者提供实用的代码示例。

笔记,自己用:

1、同类中


					Bundle bundle=new Bundle();
                    bundle.putString("k2",k2);
                    bundle.putString("k2pos",k2pos);
                    bundle.putString("d2",d2);
                    bundle.putStringArrayList("extras",extras);
                    bundle.putStringArrayList("colors",colors);
                    Message msg=new Message();
                    msg.what=SQUARE_COLOR;
                    msg.obj=bundle;
                    mHandler.sendMessage(msg);


			case SQUARE_COLOR:
                    bundle= (Bundle) msg.obj;
                    String k2=bundle.getString("k2");
                    String k2pos=bundle.getString("k2pos");
                    String d2=bundle.getString("d2");

                    ArrayList<String> extras=bundle.getStringArrayList("extras");
                    ArrayList<String> colors=bundle.getStringArrayList("colors");
                    break;

2、intent中


复杂对象:
public class DeviceInfo implements Serializable {
    String Name;
    String Type;
    String Serial;
    String Version;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getType() {
        return Type;
    }

    public void setType(String type) {
        Type = type;
    }

    public String getSerial() {
        return Serial;
    }

    public void setSerial(String serial) {
        Serial = serial;
    }

    public String getVersion() {
        return Version;
    }

    public void setVersion(String version) {
        Version = version;
    }
}

在activity中使用:
	 case R.id.living_layout_baseinfo:
                DeviceInfo deviceInfomy=new DeviceInfo();
                deviceInfomy.setName(deviceInfo.getDeviceName());
                deviceInfomy.setSerial(deviceInfo.getDeviceSerial());
                deviceInfomy.setType(deviceInfo.getDeviceType());
                deviceInfomy.setVersion(deviceInfo.getDeviceVersion());
                Bundle bundle=new Bundle();
                bundle.putSerializable("deviceinfo",deviceInfomy);
                Intent intent_baseinfo=new Intent(this,BaseInfoActivity.class);
                intent_baseinfo.putExtras(bundle);
                startActivity(intent_baseinfo);
                break;


在另一个activity中接收对象
 private void initIntentData() {
        Bundle bundle=getIntent().getExtras();
        DeviceInfo info= (DeviceInfo) bundle.getSerializable("deviceinfo");

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值