Android-Message发送时传送bundle数据

最近开发过程中用到传送bundle的数据发现数据无法发送过去

错误用法:

赋值:

   Bundle b = new Bundle();
    b.putInt("type" ,type);
    b.putInt("offset" ,offset);
     b.putInt("maxcount" ,maxcount);
     sendMessage(MSG_PHONEBOOK_DOWNLOAD ,b);

将bundle对象直接给到msg的obj

解析:

Bundle b = (Bundle)msg.obj;
 int type = b.getInt("type");
  int offset = b.getInt("offset");
 int count = b.getInt("maxcount");
 Log.d(TAG ,"MSG_DOWNLOAD_EX type = " + type + " offset " + offset + " count " + count);

数据无法解析出来。

 

正确用法:将bundle对象赋值到msg的data中

  Message msg = obtainMessage(MSG_PHONEBOOK_DOWNLOAD);;
            Bundle b = new Bundle();
            b.putInt("type" ,type);
            b.putInt("offset" ,offset);
            b.putInt("maxcount" ,maxcount);
            msg.setData(b);
            sendMessage(msg);

解析的时候获取data数据

 Bundle b = msg.getData();
int type = b.getInt("type");
 int offset = b.getInt("offset");
 int count = b.getInt("maxcount");
Log.d(TAG ,"MSG_DOWNLOAD_EX type = " + type + " offset " + offset + " count " + count);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值