How to send sms to multiple contacts and get the result code for each of them in android

隐身守侯 提交于 2019-12-18 07:24:36

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!