6

我已经阅读了很多关于发送 SMS 和多部分 SMS 消息的帖子,例如:

在 Android 中发送短信、在 Android发送和接收短信和彩信(Kit Kat Android 4.4 之前)Android PendingIntent 额外内容,BroadcastReceiver 不接收

...和其他人,但他们似乎并没有检查消息的所有部分是否已成功发送。

据我了解,对于多部分消息,您为每个消息部分添加一个 PendingIntent ,但我看到的示例似乎并没有检查所有部分是否已成功发送......如果是(我猜是第一个)他们似乎认为它是成功的......

所以我想我会发送一条多部分消息并跟踪这些部分。在成功发送所有部分之前,我不会说消息已成功发送。

我试图在以下代码中执行此操作... SmsMessageInfo 只是一个简单的类,其中包含电话号码、消息和处理的部件的布尔列表以及成功发送的部件列表,它还有一个唯一的消息 ID。

我尝试了以下方法:

private void sendLongSmsMessage(Context context, final SmsMessageInfo messageInfo) {

    // Receive when each part of the SMS has been sent (or does it????)
    context.registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String messageIdAsString = intent.getExtras().getString(INTENT_EXTRA_MESSAGE_ID_KEY);

            Log.i("SMSMessageSender", "Broadcast Intent Recieved, IntentMessageID: " + messageIdAsString + "  messageInfoId: " + messageInfo.messageId);

            if (messageIdAsString != null) {
                if (Long.parseLong(messageIdAsString) == messageInfo.messageId) {
                    String messagePartNrAsString = (String) intent.getExtras().get(INTENT_EXTRA_PART_NR_KEY);
                    int messagePartNr = Integer.parseInt(messagePartNrAsString);

                    Log.i("SMSMessageSender", "Broadcast Intent Recieved Multi Part Message Part No: " + messagePartNrAsString);

                    // We need to make all the parts succeed before we say we have succeeded.
                    switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        messageInfo.partsSent.add(messagePartNr, true);
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        messageInfo.failMessage = "Error - Generic failure";
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        messageInfo.failMessage = "Error - No Service";
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        messageInfo.failMessage = "Error - Null PDU";
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        messageInfo.failMessage = "Error - Radio off";
                        break;
                    }

                    messageInfo.partsProcessed.add(messagePartNr, true);

                    boolean allSent = true;
                    for (Boolean partSent : messageInfo.partsSent) {
                        allSent = allSent && partSent;
                    }
                    messageInfo.sent = allSent;

                    boolean allProcessed = true;
                    for (Boolean partProcessed : messageInfo.partsProcessed) {
                        allProcessed = allProcessed && partProcessed;
                    }

                    if (allProcessed) {
                        // We have our response for all of our message parts, so we can unregister our receiver.
                        Log.i("SMSMessageSender", "All message part resoponses received, unregistering message Id: " + messageIdAsString);
                        context.unregisterReceiver(this);
                    } 
                } else {
                    Log.w("SMSMessageSender", "Received a broadcast message with Id for a different message");
                }
            } else {
                Log.w("SMSMessageSender", "Received a broadcast message but not for message Id");
            }
        }
    }, new IntentFilter(SENT));

    ArrayList<String> messageList = SmsManager.getDefault().divideMessage(messageInfo.message);
    ArrayList<PendingIntent> pendingIntents = new ArrayList<PendingIntent>(messageList.size());

    messageInfo.partsSent.clear();

    for (int i = 0; i < messageList.size(); i++) {
        messageInfo.partsSent.add(i, false);
        messageInfo.partsProcessed.add(i, false);

        Intent sentIntent = new Intent(SENT);
        sentIntent.putExtra(INTENT_EXTRA_MESSAGE_ID_KEY, Long.toString(messageInfo.messageId));
        sentIntent.putExtra(INTENT_EXTRA_PART_NR_KEY, Integer.toString(i));

        Log.i("SMSMessageSender", "Adding part " + i + " tp multi-part message Id: " + messageInfo.messageId);
        pendingIntents.add(PendingIntent.getBroadcast(context, 0, sentIntent,  PendingIntent.FLAG_ONE_SHOT));
    }

    Log.i("SMSMessageSender", "About to send multi-part message Id: " + messageInfo.messageId);
    SmsManager.getDefault().sendMultipartTextMessage(messageInfo.phoneNumber, null, messageList, pendingIntents, null);
}

这样做的问题是第二个消息部分永远不会被接收到。

不得不费心费力地创建多个 PendingIntent 只是为了不去检查它们是否都有效,这似乎很奇怪。

在我显示 Message Part No 的日志消息中,它始终为 0,我从未得到第二部分,因此此代码永远不会认为它已完成。

我是不是让它太复杂了,我是否应该只接受任何返回的旧 PendingIntent 并假设其余部分同样适用(在这种情况下,为什么谷歌首先让你提供它们的列表)。

我为这个冗长的问题道歉,但我真的不知道如何以更短的方式更清楚地问:-)

问候科林

4

2 回答 2

7

这是我最终使用的代码,它混合了我的原始代码和 Bolton 的代码(感谢 Bolton :-))。

新代码可能会更好地检测到某个部分的失败,有点难以测试,上面的代码将每个消息部分的 anyError 设置为 false,所以如果第 1 部分失败并且第 2 部分成功,它可能会认为整个事情成功了......下面的代码调用(我的)messageInfo.fail,如果后续消息部分成功,它将不会被重置......我想这完全不需要,因为你会认为如果一个部分有效,其余部分也会......无论如何,下面是我最终使用的代码。

编辑 > 更新代码以删除意图中的 Extras,因为在重负载下,多个意图(我认为)合并为一个(使用不同的 PendingIntent 标志没有帮助)。最后,我为每条消息使用了不同的意图操作(例如 new Intent(SENT + messageInfo.getMessageId())),这样接收者肯定只会获得自己消息的广播。似乎在重负载下工作得更好。

谢谢。

private void sendLongSmsMessage4(Context context, final SmsMessageInfo messageInfo) {

    // Receive when each part of the SMS has been sent
    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // We need to make all the parts succeed before we say we have succeeded.
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                messageInfo.fail("Error - Generic failure");
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                messageInfo.fail("Error - No Service");
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                messageInfo.fail("Error - Null PDU");
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                messageInfo.fail("Error - Radio off");
                break;
            }

            nMsgParts--;
            if (nMsgParts <= 0) {
                // Stop us from getting any other broadcasts (may be for other messages)
                Log.i(LOG_TAG, "All message part resoponses received, unregistering message Id: " + messageInfo.getMessageId());
                context.unregisterReceiver(this);

                if (messageInfo.isFailed()) {
                    Log.d(LOG_TAG, "SMS Failure for message id: " + messageInfo.getMessageId());
                } else {
                    Log.d(LOG_TAG, "SMS Success for message id: " + messageInfo.getMessageId());
                    messageInfo.setSent(true);
                }
            }
        }
    };

    context.registerReceiver(broadcastReceiver, new IntentFilter(SENT + messageInfo.getMessageId()));

    SmsManager smsManager = SmsManager.getDefault();

    ArrayList<String> messageParts = smsManager.divideMessage(messageInfo.getMessage());
    ArrayList<PendingIntent> pendingIntents = new ArrayList<PendingIntent>(messageParts.size());
    nMsgParts = messageParts.size();

    for (int i = 0; i < messageParts.size(); i++) {
        Intent sentIntent = new Intent(SENT + messageInfo.getMessageId());
        pendingIntents.add(PendingIntent.getBroadcast(context, 0, sentIntent, 0));
    }

    Log.i(LOG_TAG, "About to send multi-part message Id: " + messageInfo.getMessageId());
    smsManager.sendMultipartTextMessage(messageInfo.getPhoneNumber(), null, messageParts, pendingIntents, null);
}
于 2013-05-21T01:54:21.910 回答
0

我正在使用下面的代码向多个联系人发送多部分消息,并且工作正常:

    // Send the SMS message
    SmsManager sms = SmsManager.getDefault();
    ArrayList<String> parts = sms.divideMessage(content);
        final int numParts = parts.size();

    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
    sentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            Log.d(TAG, "SMS onReceive intent received.");
            boolean anyError = false;
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
            case SmsManager.RESULT_ERROR_NO_SERVICE:
            case SmsManager.RESULT_ERROR_NULL_PDU:
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                anyError = true;
                break;
            }
            msgParts--;
            if (msgParts == 0) {
                hideProgressBar();
                if (anyError) {
                    Toast.makeText(context,
                            getString(R.string.sms_send_fail),
                            Toast.LENGTH_SHORT).show();
                } else {
                    //success

                }

                unregisterReceiver(sentReceiver);
            }

        }
    };
    registerReceiver(sentReceiver, new IntentFilter(SENT_ACTION));

    // SMS delivered receiver
    // registerReceiver(new BroadcastReceiver() {
    // @Override
    // public void onReceive(Context context, Intent intent) {
    // Log.d(TAG, "SMS delivered intent received.");
    // }
    // }, new IntentFilter(DELIVERED_ACTION));

    for (int i = 0; i < numParts; i++) {
        sentIntents.add(PendingIntent.getBroadcast(this, 0, new Intent(
                SENT_ACTION), 0));
    }

    for (PhoneInfo phone : recipientsList) {
        sms.sendMultipartTextMessage(phone.num, null, parts, sentIntents,
                null);
    }
    msgParts = numParts * recipientsList.size();
    showProgressBar();
于 2013-05-20T06:22:23.203 回答