1

Im trying to send sms programatically in my android application using AsyncTask in doInBackground i have a list of recipients to which i want to send sms, problem is if i send them inside the for loop it has an erratic behavior some times the app crashes some time the messages are not sent... Im using PendingIntent to make sure the message leaves the device,

 smsManager.sendTextMessage("Mobile_NUmber",null, "MessageText", sentPI, null);

can i implement some sort of mechanism in which i can send next sms only after i receive the Broadcast of first sms PendingIntent

John x
  • 4,031
  • 8
  • 42
  • 67
  • Please see my answer to this question: http://stackoverflow.com/questions/19083158/send-sms-until-it-is-successful/19084559#19084559 – cYrixmorten Mar 18 '14 at 15:36
  • @cYrixmorten tnx for your time, i surely will try to implement your solution can u please guide me how to use it for sending to multiple recipients like e.g. in for loop ... – John x Mar 18 '14 at 15:39

1 Answers1

1

See my answer to this question: Send SMS until it is successful

To send to multiple receivers simply change the method startMessageServiceIntent to:

private void startMessageServiceIntent(String message, String[] receivers) {
    Intent i = new Intent(context, SMSSender.class);
    i.putExtra(SMSSender.EXTRA_MESSAGE, message);
    i.putExtra(SMSSender.EXTRA_RECEIVERS, receivers);
    startService(i)
}

I remember spending quite a while making this implementation and recall running into some not very obvious problems, such as the need to apply an ID argument (even though documentation claims that it is not being used) and having to specify PendingIntent.FLAG_CANCEL_CURRENT.

Nevertheless, hope this helps you to get it working.

Community
  • 1
  • 1
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • tnx alot ... just gonna try it and will accept anyway :) – John x Mar 18 '14 at 19:34
  • Well you should only accept it if it works :p no problem – cYrixmorten Mar 18 '14 at 19:59
  • had to tweak the code a bit but in the end it worked, just one question, for a long message i get a lot of PendingIntents, is there a way i can get a cumulative BroadCast of parts of a message... tnx for the help ... regards – John x Mar 18 '14 at 23:00
  • I would think that it only would send 1 broadcast per message, as it uses sendMultipartTextMessage, so do you mean when adding multiple recipients? You should be able to add information to the intent such as `intent.putStringExtra("receiver", receiver)` and retrieve that information in the SentMessage BroadcastReceiver using String `receiver = intent.getStringExtra("receiver")`. – cYrixmorten Mar 19 '14 at 10:52
  • tnx very much for staying with me :) ... i have managed to send additional info to the BroadcastReceiver but when part size is say 2 the `SentMessage` is called multiple(2) times instead of 1... – John x Mar 19 '14 at 11:13
  • Oh that is true, I make an array of pending intents for each part (didnt notice before). I do not know if that is necessary to do but I wanted control of the status of the individual parts. You could send two more pieces of information with the pending intent: In the for loop generating intents do 1) `sentIntents.putIntExtra("part", (i+1))` 2) `sentIntents.putIntExtra("parts_total", parts.size())`. Then when receiving the Broadcast, the full message has been successfully sent when the integer extras "part" and "parts_total" are equal. Actually I had some more management around this but still.. – cYrixmorten Mar 19 '14 at 12:34