android - 向多个收件人发送短信的通知 - 不起作用

标签 android notifications sms broadcastreceiver

很抱歉这样开始:我很沮丧。 我读了很多关于如何去做的内容。已经用了 2-3 天了,还是不行。

对象是向多个收件人发送短信(相同的消息)。 发送本身有效,但通知无效。

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

我做错了什么?

代码如下所示。

谢谢, 阿杰

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


            }

最佳答案

PendingIntent如果某些细节与以前发布的相同,则 s 可以并且正在被系统重用。创建 PendingIntent其中仅包含Intent的 extras 不同不会导致系统更新原来的,除非你传递一个标志来表示一样多。

您应该传递不同的请求代码 - 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 类成员,并在循环之前只注册一次,而不是为每条消息注册和取消注册一个新成员。

关于android - 向多个收件人发送短信的通知 - 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27587615/

相关文章:

android - MK802 - Android 4.0 Mini PC - 未使用 'adb devices' 命令列出 - 如何安装我的应用程序

android - 如何在不 root 的情况下禁用 android 设备的 hdmi 端口?

java - 如何在android中注册SmsManager

iphone - 有没有办法在没有用户提示的情况下从 iPhone 应用程序发送短信?

php - 发送短信以从数据库 Kannel 中删除用户

android - 如何修复 recyclerview 适配器的这种奇怪行为?

java - 如何在android中解析这个Json数组

javascript - 通过 Service Worker 收到的 Chrome 通知是批量的还是实时的?

android - 如何在收到广播后在android上弹出并要求用户输入

java - Android通知进度条卡住