好吧,经过一些广泛的实验,我发现了一些东西。主要问题是一条消息只能使用一次,即它不是一个可重复使用的信封,您可以放入新的信件并再次发送。此外,请确保捆绑密钥是唯一的。请注意,我还发现了用于存储 dialogID 的 msg.what。
处理程序
private Handler uiMsgHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.i(TAG, "%%%% in the uiMsgHandler");
// msg.what() contains the dialogID. The bundle key is the itemID
if (msg != null) {
int DlgId = 0;
int ItemId = 0;
String value = ""; //leave it to each case to recast the value
String element = "";
Bundle b = new Bundle();
Set<String> s = null;
b = msg.getData();
Log.i(TAG,"bundle is "+b);
DlgId = msg.what;
Log.i(TAG,"DigId is "+DlgId);
switch (DlgId) {
case 1:
s = b.keySet(); //find the set of keys in this message bundle
Log.i(TAG,"s is "+s.toString());
Log.i(TAG,"s.size is "+s.size());
if (s.size() < 100) { // we allow up to 100 items, 99 is reserved
Iterator itr = s.iterator();
while (itr.hasNext()) {
element = (String) itr.next();
ItemId = Integer.parseInt(element.substring(0,1));
value = b.getString(element);
Log.i(TAG,"ItemID is "+ItemId+" value is "+value+" itr.next "+itr.hasNext());
// now we can handle each specific item
switch (ItemId) {
case 1:
Log.i(TAG,"Bundle data for Dialog "+DlgId+" Item "+ItemId+" is "+value);
// do something
// close the dialog
break;
case 2:
Log.i(TAG,"Bundle data for Dialog "+DlgId+" Item "+ItemId+" is "+value);
// do something
// close the dialog
break;
case 99:
Log.i(TAG,"Bundle data for Dialog "+DlgId+" Item "+ItemId+" is "+value);
// Cancel button was pressed
// close the dialog
break;
default: /* item out of range */
Log.i(TAG,"Error ItemID OoR: DialogID "+DlgId+" with message item id "+ItemId+" is out of range");
// close the dialog and toast w/ message
break;
} // end of ItemID switch
} // end of ItemID iterator while
} else { // end of number of items size check
Log.i(TAG,"too many items, must be < 100 but is "+s.size());
}
break; // end of case 1
default: /* dialog id was out of range */
Log.i(TAG,"Error: dialog id was out of range");
Log.i(TAG,"Bundle data for Dialog "+DlgId+" Item "+ItemId+"is "+value.substring(2));
// close the dialog
// toast with a message
break;
} // end of Dialog switch
} // msg null check
}
};
==================================================== ====================
自定义对话框的一部分
acceptButton = (Button) dialogview.findViewById(r_id_accept);
rejectButton = (Button) dialogview.findViewById(r_id_reject);
acceptButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Inform the user the button has been clicked
Log.i(TAG, "ACCEPT BUTTON PRESSED");
// now we are going to do the messaging
// addOptQty is item id 01
// addOptPrice is item id 02
// msg.what contains the dialogID
msg.what=id;
b.putString("1",""+addOptQty);
b.putString("2",""+addOptPrice);
msg.setData(b);
// now send the message to the handler
try {
mResponseHandler.sendMessageDelayed(msg,10);
} catch (Exception e) {
Log.i(TAG, "ERROR SENDING MESSAGE");
}
}
});
rejectButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
//Inform the user the button has been clicked
Log.i(TAG,"LOCI BUTTON PRESSED");
msg.what=id;
b.putString("99","CANCEL");
msg.setData(b);
// now send the message to the handler
try {
mResponseHandler.sendMessageDelayed(msg,10);
} catch (Exception e) {
Log.i(TAG, "ERROR SENDING MESSAGE");
}
}
});