3

I am trying to send a couple of messages in my AsyncTask and I want to track progress of sent messages. Thanks to excellent post by @cYrixmorten on this topic https://stackoverflow.com/a/19084559/3339562 I have done most of the things I wanted. Here is my code:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        sentReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context c, Intent in) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    break;
                default:
                    break;
                }
            }
        };
        getActivity().registerReceiver(sentReceiver, new IntentFilter(
                SENT));
    }


    public class SomeAsyncTask extends
            AsyncTask<ArrayList<Person>, Integer, Void> {
        private ProgressDialog dialog;
        private Context context;

        public SomeAsyncTask(Activity activity) {
            context = activity;
            dialog = new ProgressDialog(context);
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        }

        protected void onPreExecute() {
            this.dialog.show();
        }

        @Override
        protected Void doInBackground(ArrayList<Person>... params) {
            dialog.setMax(params[0].size());
            SmsManager mySMS = SmsManager.getDefault();

            for (int i = 0; i < params[0].size(); i++) {
                    Intent sentIn = new Intent(SENT);
                    PendingIntent sentPIn = PendingIntent.getBroadcast(context,
                            i, sentIn, 0);
                    mySMS.sendTextMessage(params[0].get(i).getNum(),
                                null, message, sentPIn, null);

// HERE I NEED TO WAIT TO RECEIVE CONFIRMATION THAT MESSAGE IS SENT AND 
// THEN I CAN PUBLISH PROGRESS. HOW TO DO THAT RESULT IN onReceive() IN BroadcastReceiver()?

                    publishProgress((int) ((i / (float) params[0].size()) * 100));

            }

            return null;

        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            dialog.setProgress(values[0]);
        }

        @Override
        protected void onPostExecute(Void v) {

        }
}

I put explanation inside my code. So, I need to know is message sent before I can continue sending other messages. What would be the best way to accomplish that?

Community
  • 1
  • 1
user3339562
  • 1,325
  • 4
  • 18
  • 34
  • 1
    Maybe to set some global flag to `false` and then wait for `onReceive` to set it to `true`? – user3304086 Jul 24 '14 at 19:09
  • @user3304086, It works! I set one global boolean variable and inside AsyncTask I placed one infinite 'while' loop which exits only if that boolean variable is set to 'true' inside 'onCreate()'. – user3339562 Jul 24 '14 at 20:11

0 Answers0