【问题标题】:Android: send SMS to multiple recepients and get confirmationAndroid:向多个收件人发送短信并获得确认
【发布时间】:2014-12-18 06:49:12
【问题描述】:

我想发送给多个收件人。 我还想使用内置的 SMS 机制,而不需要提示所需的应用程序(Whatsapp 等)

为了完成这个,我正在使用 Android 的 SmsManager。

for循环遍历手机号码的mobileList数组,并一一发送短信给每个手机号码。

BroadcastReceiver 检索已交付 SMS 的指示,以用于交付的ActionIntent 意图。

我正在祝酒词“已交付”和正在交付的消息的索引号。

我的问题是:

  1. 未显示实际索引 (idx)。我为所有 toasts 获得相同的索引号,即 mobileList 项目的数量。 为什么会这样?我希望每个手机本身都有索引。

  2. mobileList 项目的数量有限制吗?例如,我可以有 200 人吗?

  3. 我在一个包含 4 个手机号码的列表上对此进行了测试,但随后我得到了 8-10 个祝酒词。我希望为一次移动交付干杯。 这里有什么问题?

  4. 如何在所有短信发送完毕后收到通知?我想这应该是像 AsyncTask 这样的后台操作。 有人可以告诉我怎么做吗?

SmsManager 的代码如下所示。

SmsManager smsManager = SmsManager.getDefault();

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

    String toNumber = mobileList[idx];
    String sms = message;

    // SMS sent pending intent
    Intent sentActionIntent = new Intent(SENT_ACTION);
    sentActionIntent.putExtra(EXTRA_IDX, idx);
    sentActionIntent.putExtra(EXTRA_TONUMBER, toNumber);
    sentActionIntent.putExtra(EXTRA_SMS, sms);
    PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0, sentActionIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    /* 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;
            }

            // Toast.makeText(getApplicationContext(), result,  Toast.LENGTH_SHORT).show();
        }

    }, new IntentFilter(SENT_ACTION));                  


    // SMS delivered pending intent
    Intent deliveredActionIntent = new Intent(DELIVERED_ACTION);
    deliveredActionIntent.putExtra(EXTRA_IDX, idx);
    deliveredActionIntent.putExtra(EXTRA_TONUMBER, toNumber);
    deliveredActionIntent.putExtra(EXTRA_SMS, sms);
    PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(this, 0, deliveredActionIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    /* Register for Delivery event */
    registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(getApplicationContext(), "Deliverd " + Integer.toString(idx), Toast.LENGTH_SHORT).show();
        }

    }, new IntentFilter(DELIVERED_ACTION));

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

【问题讨论】:

    标签: android sms confirmation smsmanager


    【解决方案1】:

    1) idx 在你运行 for 循环时发生变化。因此,每次您敬酒时,都会显示 idx 的当前值,即显示的消息数。由于您已将其打包到您的意图中,因此您可以在 onReceive 方法中简单地显示文本 "Delivered" + intent.getIntExtra(EXTRA_IDX, -1)

    2) 我不确定你在问什么。

    3) 我不确定,目前无法调试。

    4) 您必须跟踪收到的索引。 HashSet&lt;Integer&gt; 应该可以解决问题。

    在你的 for 循环之上,添加:

    final HashSet<Integer> undelivered = new HashSet<Integer>();
    

    在你的 for 循环中,添加:

    undelivered.add(idx);
    

    要同时回答关于 1、3 和 4 的问题,请将您的 onReceived 正文更改为:

    // Get the index from the intent
    int idx = intent.getIntExtra(EXTRA_IDX, -1);
    
    if (undelivered.contains(idx)) {
        // This index is now delivered. We remove it from the undelivered set, and Toast that it was delivered.
        undelivered.remove(idx);
        Toast.makeText(getApplicationContext(), "Delivered " + idx, Toast.LENGTH_SHORT).show();
    
        if (undelivered.isEmpty() {
            // We've delivered all of the messages ...
            Toast.makeText(getApplicationContext(), "All messages were delivered.", Toast.LENGTH_SHORT).show();
        }
    }
    

    【讨论】:

    • 非常感谢您的回答。对不起,我花了这么长时间才回答。我使用了您建议的代码并添加了一些日志来解释它对我不起作用。我写了一些日志。如果我在列表中有两个电话号码,则接收广播被调用两次以上...... undelivered.add 正确添加了 idx,但 undelivered.remove 被调用了两次以上。此外,它使用常量索引:0 调用。这导致 undelivered.isEmpty 永远不会激活。我烦了。不知道如何解决这个问题。你有什么想法吗?
    • 非常感谢。我最终得到了它,并将你的想法用于未交付的哈希 :-) 非常感谢!
    • 我认为你在这里遗漏了一些东西。 BroadcastReciever 应该只注册一次,而不是在 for 循环中,这会导致崩溃
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多