问题
Below link tells how to send sms to single contact and get resultcodelink
but How to send sms to multiple contacts and get the result code for each of them in android
回答1:
You could use a for loop and create a new BroadcastReceiver each time the for loop executes, and have the contact list be an array list, and each time you send the message to a new contact, use contactList.get(i) as follows:
SmsManager smsMan = new SmsManager.getDefault();
ArrayList<String> contactList = new ArrayList();
//add contacts to contactList with contactList.add(string)
for (int i = 0; i <= contactList().size(); i++) {
String SENT = contactList.get(i).toString();// you could replace this with i,
//or something like "sms_sent_myappname" + i.toString());
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT, 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
smsManager.sendTextMessage(contactList.get(i).toString(), null, message, sentPI, null);
}
I haven't tested it, but it seems like it would work.
来源:https://stackoverflow.com/questions/7092136/how-to-send-sms-to-multiple-contacts-and-get-the-result-code-for-each-of-them-in