Andoxko
Andoxko

Reputation: 1031

AsyncTask not calling onProgressUpdate,onPostExecute

I am trying to understand the use of the AsyncTask on Android and with that purpose I have developed this simple code.

And surprisingly doInbackground is called correctly and I see the logs but the other two methods are not called.

Am I missing something?

  public class DownloadFilesTask extends AsyncTask<Void, Void, Void> {

        protected Void doInBackground(Void... params) {
            try {
                Log.i("Thread","1");
                Thread.sleep(1000);
                Log.i("Thread","2");
                Thread.sleep(1000);
                Log.i("Thread","3");
                Thread.sleep(1000);
                Log.i("Thread","4");
                Thread.sleep(1000);
                Log.i("Thread","5");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onProgressUpdate(Void... progress) {
            Log.i("Thread","onProgress");
        }

        protected void onPostExecute(Void... result) {
            Log.i("Thread","onPosts");
        }
    }

[EDIT]

With this code all works right excepting the onProgressUpdate

public class DownloadFilesTask extends AsyncTask<Void, Integer, String> {

public String doInBackground(Void... params) {
    try {
        int i = 0;
        Log.i("Thread","1");
        Thread.sleep(1000);
        publishProgress(i++);
        Log.i("Thread","2");
        Thread.sleep(1000);
        publishProgress(i++);
        Log.i("Thread","3");
        Thread.sleep(1000);
        Log.i("Thread","4");
        Thread.sleep(1000);
        Log.i("Thread","5");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "done";
}

public void onProgressUpdate(int progress) {
    Log.i("Thread","onProgress " + progress);
}

public void onPostExecute(String result) {
    Log.i("Thread","onPosts " + result);
}

And here you can see a screenshot of my LogCat:

Log

Upvotes: 4

Views: 4530

Answers (2)

Blackbelt
Blackbelt

Reputation: 157437

The method signature is wrong. It should look like:

protected void onPostExecute(Void result) {
    Log.i("Thread","onPosts");
}

also, I suggest you to use the @Override annotation. This way you will get a compile time error if you are trying to overriding a wrong method.

Edit: The signature of onProgressUpdate you posted is wrong. It should be:

protected void onProgressUpdate(Integer... progress){}

Upvotes: 5

A.S.
A.S.

Reputation: 4574

AsyncTask has method onProgressUpdate(Void...) that you can call each iteration for example or each time a progress is done during doInBackground() by calling publishProgress().

Refer to the docs for more details

So it should look lik this:

public class DownloadFilesTask extends AsyncTask<Void, Integer, String> {

    protected String doInBackground(Void... params) {
        try {
            int i = 0;
            Log.i("Thread","1");
            Thread.sleep(1000);
            publishProgress(i++);
            Log.i("Thread","2");
            Thread.sleep(1000);
            publishProgress(i++);
            Log.i("Thread","3");
            Thread.sleep(1000);
            Log.i("Thread","4");
            Thread.sleep(1000);
            Log.i("Thread","5");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "done";
    }

    protected void onProgressUpdate(int progress) {
        Log.i("Thread","onProgress " + progress);
    }

    private void onPostExecute(String result) {
        Log.i("Thread","onPosts " + result);
    }
}

Upvotes: 1

Related Questions