Sending sms to multiple people in android

后端 未结 2 580
借酒劲吻你
借酒劲吻你 2020-12-05 03:37

I want to know if there is anyway that I can send sms to multiple number of people using the SmsManager. I know that I can run a loop through the contacts and send sms indiv

2条回答
  •  执念已碎
    2020-12-05 04:05

    The net-net-net here is it cannot be done without iterating through a loop and sending 1 message to 1 addressee.

    I spent 1/2 a Saturday trying to do this very thing. I could not make it work with ";", ",", " ", or "\n". I should have tried hard-coding 2 addressees separated by all the delimiters first, but I did learn a valuable lesson about the Android SDK: if they wanted you to send to more than 1 addressee at a time then they'd accept an ArrayList or an array of Strings rather than a singular String ;)

    protected void sendMsg(Context context, SmsMessage smsMessage) {
            SmsManager smsMgr = SmsManager.getDefault();
            ArrayList smsMessageText = smsMgr.divideMessage(smsMessage.getMsgBody());
            PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0);
            PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
            int AddresseesPerMessage = 10;
            StringBuilder builder = new StringBuilder();
            String delim = "";
            for (ContactItem c:smsMessage.getAddresseeList()) {
                //  For every phone number in our list
                builder.append(delim).append(c.getPhoneNumber().toString());
                delim=";";
                if (((smsMessage.getAddresseeList().indexOf(c)+1) % AddresseesPerMessage) == 0 || smsMessage.getAddresseeList().indexOf(c)+1 == smsMessage.getAddresseeList().size()) {
                    // using +1 because index 0 mod 9 == 0 
                    for(String text : smsMessageText){
                        //  Send 160 bytes of the total message until all parts are sent
                        smsMgr.sendTextMessage(builder.toString(), null, text, sentPI, deliveredPI);
                    }
                    builder.setLength(0);
                    delim="";
                }
            }
        }
    

提交回复
热议问题