1

I am practicing the working of the AsyncTask and with that purpose I wanted to use the onProgressUpdate function. Currently in my program I have UI that lets the user choose an input (which would be show in a TextView after the AsyncTask is finished) and a timer (determines Thread.sleep() time in the AsyncTask)

What I want to do is ... if the user selects a time of 5. Then in every passing second I would like to send a notification to the UI (where a progress dialog is called) ... indicating progress of 1 of 5 ... 2 of 5 ... 3 of 5.

Here's the progress that I have made so far:

public class ViewFiller extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... input) {
        // TODO Auto-generated method stub
        try {
            int time;
            if (input[1].equalsIgnoreCase("")) {
                time = 0;
            } else {
                time = Integer.parseInt(input[1]) * 1000;
                for (int i = 1; i <= time / 1000; i++) {
                    publishProgress(i, time);
                }
                Thread.sleep(time);
            }

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return input[0];
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        execute.setText("Execute");
        progress.dismiss();
        tvInput.setText(result);
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        execute.setText("Running...");
        displayProgress("Updating field ... ");
        // Start the progress dialog
        progress.show();

    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        progress.setMessage("Updating field ... " + values[0] + " of "
                + values[1] / 1000);
    }

}

The current implementation only gives me 5 of 5 directly. Can anyone suggest me a solution ?

Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74

1 Answers1

2

Because it is going through your loop faster than you see it

for (int i = 1; i <= time / 1000; i++) {
                publishProgress(i, time);

You don't need the loop there. Just sleep for whatever amount of time then show your progress and have the sleep() and the publishProgress() in the loop. Something like

try {
       int time = 0;
       for (int i = 1; i <= time / 1000; i++) {

        if (input[1].equalsIgnoreCase("")) {
            time = 0;
        } else {
            time = Integer.parseInt(input[1]) * 1000;
                publishProgress(time);
            }
            Thread.sleep(time);
        }

Although, I'm not sure what input actually contains but you might want input[i]. It looks like its always going to be the same otherwise.

Also, CountDownTimer would be good for this I would think.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • @user2247689 the edit had already been rejected but I think either way would work, at least with some tweaking maybe. The important part is that the `sleep()` is inside the `loop` and that it is working. – codeMagic Nov 12 '13 at 02:50