Ok, so i am making a Android app that is able to send sms msgs multiple times with intervals between the amount of messages is sent. This is partially working but it sometimes does not. For example when i input 20 msgs to send it sends 16 or maybe a little more. Also when i push the button it freezes until it finishes sending the SMS’s.is there anyway that someone could tell me what is wrong with my code? i have tried a few fixs that are not working very well and thought i would come here for help after many failed trys at this. PS there is more code then this but this is where the issue resides i beleive. Thanks for reading
EDIT: FIXED!
Ok, so i am making a Android app that is able to send sms
Share
The button is “freezing” because the UI thread is sending all 20 SMS before it frees up to do other things.
I would look into AsyncTask. This is Android’s way to easily thread. Essentially what you would do it spawn a new thread in the background with AsyncTask. This frees up the UI thread so that it looks like everything is normal to the user (no freezing), while the AsyncTask is doing all of the work in the background with
doInBackground. Then when the Task is done useonPostExecuteto notify the UI that it is done and perhaps tell the user something. You’d put the call toMyAsyncTask.execute(smsDataHere)in the onClick.That link to AsyncTask is fairly comprehensive for simple purposes and has example code.
I’m not sure exactly why it only send 16 or so messages, but I can see right off the bat that you are missing some curly braces and that they don’t quite match up the way you want them to. I would look there first. Also, is there a reason for the second
forloop?sizeis always 1, so it is just sending each message twice.EDIT:
Here is some sample code. I’m not going to promise it works in your exact app, but it will show you how the AsyncTask works and give you a starting point. I also tried to fix any problems with the curly braces and did a little cleaning up. I still recommend looking over the documentation to make sure you understand how it works. I hope this helps you.