Here is the code I have ended up using, it is a mix of my original and that of Bolton (thanks Bolton :-)).
The new code may detect a failure of a part better, a bit hard to test, the code above sets anyError to false for each message part, so if part 1 failed and part 2 succeeded it might think that the whole thing succeeded... the code below calls (my) messageInfo.fail which won't get reset if a subsequent message part succeeds... I imagine this is all pretty unneeded as you would think that if one part works, the rest will too... anyhow, below is the code I ultimately used.
Edit > Updated the code to remove the Extras in the intent as under heavy load, multiple intents (I think) got merged into one (using different PendingIntent flags did not help). In the end, I used a different intent action for each message (e.g. new Intent(SENT + messageInfo.getMessageId())), this way the receiver definitely only gets broadcasts for its own message. Seems to work better under heavy load.
Thanks.
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);
}