10

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 individually but I figured that there may be a way to do this.

The code I use is given below:

SmsManager.getDefault().sendTextMessage("PHONE_NOS", null,msg.getText().toString(), sentPI, deliveredPI);

PS: I have tried using ";" as a separator. But the only thing that happens is that it sends an sms only to the first person in the list.

For the benefit of people who are seeing this late, its not possible to send SMS to multiple people. As Bill Mote has pointed out, if there was such a thing possible, there would have been an API which would have taken a "List-of-numbers" as an argument. So the only possible solution is to have an iterator for the numbers and send them one at a time.

Anand Sainath
  • 1,807
  • 3
  • 22
  • 48

2 Answers2

10

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<string> 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="";
            }
        }
    }
Bill Mote
  • 12,644
  • 7
  • 58
  • 82
  • Can u explain what you have done?? Like what is AddresseesPerMessage (What if I give more than 10 addresses?). – Anand Sainath Mar 27 '11 at 13:18
  • It was just an example of how I tried to work through the very same problem. You cannot have more than 1 addressee per message. AddresseesPerMessage was set to 10 because I was trying to bundle together 10 addressees in a delimited string like this, "1234567890,2345678901,3456789012, ..." – Bill Mote Mar 27 '11 at 15:10
  • Ok, so i just gotta set the delimiter string to [a comma or any other delimiter that I use] and just call the function? I will definitely try this out! Thanks! – Anand Sainath Mar 27 '11 at 15:27
  • You're missing the part of my answer and my comment that says, it CANNOT be done in the way that you and I both want to do it. – Bill Mote Mar 27 '11 at 15:39
  • SHEESH. That's a bad bad feeling!! You can add one day to that wasted count! SHEEEESH! – Anand Sainath Mar 27 '11 at 16:15
  • I know. I did it 2 weeks ago. http://www.aydabtudev.com/2011/03/sending-single-sms-to-multiple.html – Bill Mote Mar 27 '11 at 16:17
  • Well anyways, thanks a ton man- Without your help I would have been going on and on , trying desperately to make it work.. – Anand Sainath Mar 27 '11 at 16:20
9

This may helpful for you.

public void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);

      setContentView(R.layout.main);
      btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
      btnSendSMS.setOnClickListener(new View.OnClickListener()
      {
         public void onClick(View v)
          {
            Intent i = new Intent(android.content.Intent.ACTION_VIEW);
             i.putExtra("address", "5556; 5558; 5560");
             // here i can send message to emulator 5556,5558,5560
             // you can change in real device
             i.putExtra("sms_body", "Hello my friends!");
             i.setType("vnd.android-dir/mms-sms");
             startActivity(i);
     }
     });
 }

Add this line in AndroidManifest.xml

<uses-permission android:name="android.permission.SEND_SMS"/>
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
selva_pollachi
  • 4,147
  • 4
  • 29
  • 42
  • @Bill Mote is this code working cause getAddresseeList() method is not found when I am trying to implement. – Saty May 03 '14 at 09:54