Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android捆绑包和消息(有时是多个)_Android_Bundle - Fatal编程技术网

Android捆绑包和消息(有时是多个)

Android捆绑包和消息(有时是多个),android,bundle,Android,Bundle,android/java还是新手,对捆绑包、消息传递和处理程序非常困惑(如果我的术语不太正确,我深表歉意)。我有一个自定义对话框类,可以显示多个对话框。侦听器被设置为通过处理程序将数据传回主调用活动 传回的数据可能是一项或多项。在我的测试中,我试图发回两个项目。我试过很多方法。我总是成功地传输单个项目,并让处理程序接收并提取它。我在做多个项目时失败(方式略有不同) 如果我将两个项目放入捆绑包并只发送一条消息,则处理程序似乎只接收第二个捆绑包项目,而不是第一个 如果我将一个项目放入bundle,s

android/java还是新手,对捆绑包、消息传递和处理程序非常困惑(如果我的术语不太正确,我深表歉意)。我有一个自定义对话框类,可以显示多个对话框。侦听器被设置为通过处理程序将数据传回主调用活动

传回的数据可能是一项或多项。在我的测试中,我试图发回两个项目。我试过很多方法。我总是成功地传输单个项目,并让处理程序接收并提取它。我在做多个项目时失败(方式略有不同)

如果我将两个项目放入捆绑包并只发送一条消息,则处理程序似乎只接收第二个捆绑包项目,而不是第一个

如果我将一个项目放入bundle,send,清除bundle,然后将第二个项目放入bundle并发送,那么处理程序似乎没有收到任何内容,活动挂起

我还使用了msg.toString()的输出,注意如果发送了两个消息,则第二个消息的“when”为0。不知道这是否重要

此外,我还尝试使用msg.sendToTarget和handler.sendMessage(msg)传递消息,但使用哪个似乎并不重要

代码剪接并在此处输出:


我留下来了,但把其他一些尝试过的东西都注释掉了。我真的不明白我做错了什么。

经过一些广泛的实验,我发现了一些东西。主要问题是一封邮件只能使用一次,也就是说,它不是一个可重复使用的信封,您可以放入新的信件并再次发送。此外,请确保捆绑密钥是唯一的。注意,我还发现了用于存储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");
                        }



                    }
                });