我已经阅读了很多关于发送短信和多部分短信的帖子,例如:
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 – 如何检查成功的多部分短信发送全部内容,希望文章能够帮你解决android – 如何检查成功的多部分短信发送所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)