最近开发过程中用到传送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);