Reputation: 185
I'm trying to send multiple SMS messages. My code:
for (Entry<String, String> entry : book.entrySet()) {
sendSMS(entry.getValue(), message);
}
sendSMS:
private void sendSMS(final String phoneNumber, String message) {
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
System.out.println("sent");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
System.out.println("No network "+ phoneNumber);
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
}
}
}, new IntentFilter(SENT));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, null);
}
But the cases are performed several times. For 4 contcts:
What could be the reason? Thanks!
Upvotes: 1
Views: 215
Reputation: 1168
I think moving the registerReceiver(...) outside of sendSMS will solve this. This seems to be a case of multiple registrations.
Try something like this
Intent smsSentIntent = new Intent(SENT);
smsSentIntent.setClass(context, SmsSentBroadcastReceiver.class);
Upvotes: 2