将短信发送给多个收件人的通知 - 不工作

时间:2014-12-21 07:48:30

标签: android notifications sms broadcastreceiver

抱歉这样开始:我很沮丧。 我读了很多关于如何做的事。已经花了这2-3天,它仍然没有用。

目标是向多个收件人发送SMS(相同的消息)。 发送本身有效,但通知没有。

我把所有东西放在一个循环中。 BroadcastReceiver的onReceive应该每次为不同的索引(idx)烘烤,但事实并非如此。它为所有迭代提供相同的索引(0)。

我做错了什么?

代码如下所示。

谢谢, AJ

            for(idx = 0; idx < mobileList.length; idx++) {

                toNumber = mobileList[idx];
                sms = message;

                Intent sentActionIntent = new Intent(SENT_ACTION);
                sentActionIntent.putExtra(EXTRA_IDX, idx);
                PendingIntent sentPendingIntent = PendingIntent.getBroadcast(SmsActivity.this, 0, sentActionIntent, 0);

                Intent deliveredActionIntent = new Intent(DELIVERED_ACTION);
                deliveredActionIntent.putExtra(EXTRA_IDX, idx);
                PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(SmsActivity.this, 0, deliveredActionIntents, 0);


            /* Register for SMS send action */
            registerReceiver(new BroadcastReceiver() {

                @Override
                public void onReceive(Context context, Intent intent) {
                    String result = "";

                    switch (getResultCode()) {

                    case Activity.RESULT_OK:
                        result = "Transmission successful";
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        result = "Transmission failed";
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        result = "Radio off";
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        result = "No PDU defined";
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        result = "No service";
                        break;
                    }

                    SmsActivity.this.unregisterReceiver(this);
                }

            }, new IntentFilter(SENT_ACTION));                  

            /* Register for Delivery event */
            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {

                  switch (getResultCode())
                  {
                    case Activity.RESULT_OK:
                      Toast.makeText(getBaseContext(), "SMS delivered " + Integer.toString(arg1.getIntExtra(EXTRA_IDX, -1)),
                        Toast.LENGTH_SHORT).show();
                      break;
                    case Activity.RESULT_CANCELED:
                      Toast.makeText(getBaseContext(), "SMS not delivered",
                        Toast.LENGTH_SHORT).show();
                      break;
                    }

                  SmsActivity.this.unregisterReceiver(this);


                  }


                }, new IntentFilter(DELIVERED_ACTION));                 

                smsManager.sendTextMessage(toNumber, null, sms, sentPendingIntent, deliveredPendingIntent);


            }

1 个答案:

答案 0 :(得分:2)

如果某些细节与先前发布的细节相同,则系统可以重复使用

PendingIntent。创建PendingIntent,其中只有包含的Intent附加内容不同,不会导致系统更新原始内容,除非您传递一个标记以表明内容。

您应该为每个getBroadcast()创建一个不同的请求代码 - PendingIntent中的第二个参数 - 并为最后一个参数指定FLAG_UPDATE_CURRENTFLAG_CANCEL_CURRENT。例如:

PendingIntent sentPendingIntent = PendingIntent.getBroadcast(SmsActivity.this, idx, sentActionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(SmsActivity.this, idx, deliveredActionIntents, PendingIntent.FLAG_UPDATE_CURRENT);

您还应该创建Receivers类成员,并在循环之前只注册一次,而不是为每条消息注册和取消注册一个新成员。