0

i want to send messages to five different numbers. I am getting that numbers from an another page by using the getIntent() method. Can anyone please give a proper way to send the messages. i used the following code. but it toasts that " SMS sent fo first number" and then all other messages are getting failed.!! But the thing is that i didnt even recieve any message.!!

try{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage( phonenumber1 ,null,message,null, null);
Toast.makeText(getApplicationContext(), "SMS Sent to First   Number.!",Toast.LENGTH_LONG).show();
}catch(Exception e)
{
Toast.makeText(getApplicationContext(), "SMS Sent failed to First Number.!",Toast.LENGTH_LONG).show();
e.printStackTrace();

} // like this up to phonenumber5 with the same try and catch block each five times
  • If your message is more than 1 message then http://stackoverflow.com/questions/4774009/broadcast-receiver-with-sendmultiparttextmessage – Jagadesh Seeram Jan 09 '14 at 13:15
  • What name.?? are you saying.? –  Jan 09 '14 at 13:16
  • Ok. i got it.!! But i have no idea to which code that i haev to take and put. please help me out. Suggest me the correct code.?? from that example –  Jan 09 '14 at 13:19
  • How are you sending numbers through intent? In a string array? – MysticMagicϡ Jan 09 '14 at 13:22
  • refer link http://stackoverflow.com/questions/16771470/android-send-sms-to-multiple-contacts-using-arraylist – Gayathiri Jan 09 '14 at 13:23
  • Am getting numbers in five different strings. and then it put into the above sending message code. thats why used five different try and catch block for sending the message –  Jan 09 '14 at 13:25

2 Answers2

1

Try like this. Firstly, put all numbers in a string array, and use for loop instead of 5 try catch.

String receipentsNumber[] = {"111","222","333","444","555"};

for (int i = 0; i < receipentsNumber.length; i++) {

try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(receipentsNumber[i], null, message, null,
                    null);
        Toast.makeText(getApplicationContext(), "SMS Sent to" + " " + receipentsNumber[i], Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

}

As you get success for first number, I presume you have added permission in manifest.

<uses-permission android:name="android.permission.SEND_SMS"/>

Hope this helps.

MysticMagicϡ
  • 28,305
  • 16
  • 71
  • 119
0

5 try catches is not a good method. Why not put all numbers in a string array, and use for loop instead of 5 try catch

AnOldSoul
  • 3,757
  • 9
  • 49
  • 104