3

Im writing an app that sends an SMS to several contacts. The contacts numbers are stored in an ArrayList (was received from another activity). I am not able to use this ArrayList to pass several contacts to the built-in SMS android app. This is the code:

ArrayList<String> numbersArrayList=getIntent().getExtras().getStringArrayList("phoneNumbers");
String message= "this is a custom message";
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.putExtra("sms_body", message); 
smsIntent.putExtra("address", ??????????);
smsIntent.setType("vnd.android-dir/mms-sms");
startActivity(smsIntent);

I can iterate and print these contacts to the LogCat the simple "for each" loop and overriding toString method.

neo108
  • 5,156
  • 3
  • 27
  • 41
Alex
  • 1,982
  • 4
  • 37
  • 70
  • 1
    Check this post http://stackoverflow.com/questions/14065910/sending-sms-programmatically-to-multiple-people-getting-generic-error – dracula May 27 '13 at 10:50
  • refer this http://stackoverflow.com/questions/12276712/sending-sms-to-multiple-contacts-using-loop – Sunil Kumar May 27 '13 at 10:52

1 Answers1

9

Use this code..

String toNumbers = "";
for ( String s : numbersArrayList)  
{  
    toNumbers = toNumbers + s + ";"
}  
toNumbers = toNumbers.subString(0, toNumbers.length - 1);
String message= "this is a custom message";

Uri sendSmsTo = Uri.parse("smsto:" + toNumbers);
                Intent intent = new Intent(
                        android.content.Intent.ACTION_SENDTO, sendSmsTo);
                intent.putExtra("sms_body", message);
                startActivity(intent); 
bakriOnFire
  • 2,685
  • 1
  • 15
  • 27
  • thanks for the comment, but the app crashes, ive copied several lines from the log: 05-27 15:38:46.208: E/AndroidRuntime(981): java.lang.NullPointerException 05-27 15:38:46.208: E/AndroidRuntime(981): at com.example.appofthecentury.CreateEventActivity$5.onClick(CreateEventActivity.java:111) 05-27 15:38:46.208: E/AndroidRuntime(981): at android.view.View.performClick(View.java:4204) 05-27 15:38:46.208: E/AndroidRuntime(981): at android.view.View$PerformClick.run(View.java:17360) – Alex May 27 '13 at 13:23
  • to concatenate with ";", you could do this instead of a loop : String toNumbers = TextUtils.join(";", numbersArrayList); – phyzalis May 08 '14 at 10:28
  • 3
    hi Is there any limit defined by android for Contacts size?actually having more then 1000 contacts for sending sms. can it works for all thousands contancts on single send click?. – Brijesh Patel Jun 20 '14 at 06:48