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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章