android – 如何检查成功的多部分短信发送
作者:互联网
我已经阅读了很多关于发送短信和多部分短信的帖子,例如:
Sending SMS in Android,
Sending and Receiving SMS and MMS in Android (pre Kit Kat Android 4.4),
Android PendingIntent extras, not received by 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);
}
这个问题是永远不会收到第二个消息部分.
看到创建多个PendingIntents的麻烦只是为了不去检查它们是否全部工作似乎很奇怪.
在我显示消息部件号的日志消息中,它始终为0,我从未得到第二部分,因此此代码从未认为它已完成.
我只是把它弄得太复杂了,我应该把任何旧的PendingIntent带回来并假设同样适用于其余部分(在这种情况下,为什么Google会让你首先提供它们的列表).
我为这个长期问题道歉,但不知道如何以更短的方式更清楚地问:-)
问候
科林
解决方法:
这是我最终使用的代码,它是我的原始和Bolton的混合(感谢Bolton :-)).
新代码可以更好地检测部件故障,有点难以测试,上面的代码为每个消息部分设置anyError为false,所以如果第1部分失败而第2部分成功,它可能会认为整个事情成功…下面的代码调用(my)messageInfo.fail,如果后续的消息部分成功,它将不会被重置…我想这都是非常不需要的,因为你会认为如果一个部分有效,其余部分也将…无论如何,下面是我最终使用的代码.
编辑>更新了代码以在重载下移除intent中的Extras,多个意图(我认为)合并为一个(使用不同的PendingIntent标志没有帮助).最后,我对每个消息使用了不同的意图动作(例如,新的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);
}
标签:android,smsmanager 来源: https://codeday.me/bug/20190831/1779222.html