Search code examples
androidarrayssmssmsmanager

Sending SMS to an Array of numbers - Android


Im am trying to send a SMS to an array of numbers in Android however the SMS is only being sent to the first number in the array. What could be going wrong?

Here is my code:

                        android.telephony.SmsManager shortMessageManager;
                    shortMessageManager = SmsManager.getDefault();

                    // Get DB
                    dbTools = new DBTools(MainActivity.this);

                    // Get array
                    ArrayList<String> phoneNumberArray = dbTools
                            .getAllphoneNumbers();



                    String SMSNumbers = phoneNumberArray.toString();


                    String message = "Hello from Android";
                    try {
                        // Do something
                        shortMessageManager.sendTextMessage(SMSNumbers,
                                null, message, null, null);
                        Log.d("PhoneNo", SMSNumbers);
                    } catch (Exception e) {
                        Log.d("PhoneNo", "fail");
                    }

I've logged the "SMSNumbers" and it outputs each number in the array seperated by a , and a space.


Solution

  • I converted my ArrayList to an Array and then created a loop to send out the SMS

                            android.telephony.SmsManager shortMessageManager;
                    shortMessageManager = SmsManager.getDefault();
    
                    // Get DB
                    dbTools = new DBTools(MainActivity.this);
    
                    // Get array
                    ArrayList<String> phoneNumberArray = dbTools
                            .getAllphoneNumbers();
    
    
    
             String[] SMSNumbers = phoneNumberArray.toArray(new String[phoneNumberArray.size()]);   
    
    
                    String message = "Hello from Android";
    
    
                        try {
                            // Do something
                            for(int i=0;i<SMSNumbers.length;i++){
                                Thread.sleep(3000);
                            shortMessageManager.sendTextMessage(SMSNumbers[i],
                                    null, message, null, null);
                            Log.d("PhoneNo",SMSNumbers[i]);
                            }
                        } catch (Exception e) {
                            Log.d("PhoneNo", "fail");
                        }