首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android:向多个接待员发送SMS并获得确认

Android:向多个接待员发送SMS并获得确认
EN

Stack Overflow用户
提问于 2014-12-18 06:49:12
回答 1查看 818关注 0票数 0

我想派几个接待员。我还想使用内置的SMS机制,而不需要提示应用程序(Whatsapp等)。

为了解决这个问题,我使用了Android的SmsManager。

for循环遍历移动号码的mobileList数组,并一个一个地向每个移动号码发送SMS。

BroadcastReceiver为deliveredActionIntent意图检索所传递的短消息的指示。

我用“传递”这个词和正在传递的消息的索引号来祝酒。

我的问题是:

  1. 未显示实际索引(idx)。对于所有的祝酒词,我都会得到相同的索引数,也就是mobileList项的数量。为什么会发生这种情况?我预计每个移动设备的索引都是独立的。
  2. mobileList项目的数量有限吗?比如说我能要200个人吗?
  3. 我在4个手机号码的列表上测试了这个,但之后我得到了8-10个祝酒词。我本想为一次流动送货干杯的。这里怎么了?
  4. 当所有SMS都交付时,我如何获得通知?我想这应该是一个像AsyncTask这样的背景动作。有人能告诉我怎么做吗?

SmsManager的代码如下所示。

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-18 07:15:04

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

我不知道你在问什么。

3)我不确定是临时的,而且目前无法调试。

4)你必须跟踪你收到的指数。HashSet<Integer>应该能做到这一点。

在for循环的上方,添加以下内容:

代码语言:javascript
复制
final HashSet<Integer> undelivered = new HashSet<Integer>();

在for循环中,添加以下内容:

代码语言:javascript
复制
undelivered.add(idx);

若要同时回答1、3和4的问题,请将onReceived体改为:

代码语言:javascript
复制
// 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();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27540711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档