Android sms意图捆绑toast信息不更新

时间:2012-06-06 05:59:56

标签: android android-intent sms toast

我正在发送2条短信,我希望用

发送2条Toast留言

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

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);