3

我正在发送 2 条短信,我想要 2 条 toast 消息

1)“已发送短信 - 第一个号码”和

2)“已发送短信-第二个号码”

但是,我收到 2 条 Toast 消息都显示消息

1),消息发生了什么

2)? 少了什么东西?

谢谢。

    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent - " + arg1.getStringExtra("SendTo"), 
                            Toast.LENGTH_LONG).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    SmsManager sms = SmsManager.getDefault();
    Intent intent = new Intent(SENT);
    intent.putExtra("SendTo", "first number");
    PendingIntent sentIntents = PendingIntent.getBroadcast(this, 0, intent, 0);
    sms.sendTextMessage("1234", null, message, sentIntents, null);

    Intent intent1 = new Intent(SENT);
    intent1.putExtra("SendTo", "second number");
    PendingIntent sentIntents1 = PendingIntent.getBroadcast(this, 0, intent1, 0);
    sms.sendTextMessage("456", null, message, sentIntents1, null);
4

2 回答 2

1

您正在做的是 - 使用相同的意图。因此您必须更新意图并更改请求代码,以使结果不重叠。请参阅以下内容。以下代码将起作用。

 registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK:
                            Toast.makeText(getBaseContext(), "SMS sent - " + arg1.getStringExtra("SendTo"),
                                    Toast.LENGTH_LONG).show();
                            break;
                    }
                }
            }, new IntentFilter(SENT));

            SmsManager sms = SmsManager.getDefault();
            Intent intent = new Intent(SENT);
            intent.putExtra("SendTo", "first number");
            PendingIntent sentIntents = PendingIntent.getBroadcast(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
            sms.sendTextMessage("5556", null,message, sentIntents, null);

            Intent intent1 = new Intent(SENT);
            intent1.putExtra("SendTo", "second number");
            PendingIntent sentIntents1 = PendingIntent.getBroadcast(this, 1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
            sms.sendTextMessage("5556", null, message, sentIntents1, null);
于 2012-06-06T06:21:35.937 回答
0

改成这样:

Toast.makeText(this, "First number", Toast.LENGTH_SHORT).show();
sms.sendTextMessage("1234", null, message, null, null);
Toast.makeText(this, "Second number", Toast.LENGTH_SHORT).show();
sms.sendTextMessage("456", null, message, mull, null);
于 2012-06-06T06:07:59.317 回答