4

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

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Andoxko
  • 1,021
  • 2
  • 12
  • 19

2 Answers2

5

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){}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thank you very much for the answer and for the advice :) `onPostExecute ` is now working but `protected void onProgressUpdate(Void progress)` is not getting executed.. – Andoxko Dec 18 '13 at 12:16
  • I can not see in your code where you do call publishProgress(); – Blackbelt Dec 18 '13 at 13:13
  • I have edited my answer. The onProgressUpdate's signature is wrong – Blackbelt Dec 19 '13 at 15:00
  • You are completely right!! Thank you very much!! `(Integer... progress)` was the thing – Andoxko Dec 20 '13 at 07:12
  • Thanks this worked for me. @blackbelt please do you think you could give me your opinion on this question http://stackoverflow.com/questions/25598696/recommended-way-order-to-read-data-from-a-webservice-parse-that-data-and-inse – Axel Sep 02 '14 at 03:18
1

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);
    }
}
A.S.
  • 4,574
  • 3
  • 26
  • 43
  • Thank you very much for the answer and example your code works fine but `protected void onProgressUpdate(int progress)` is not getting executed.. At least I do not see the logs.. – Andoxko Dec 18 '13 at 12:18
  • This is the snapshot of my logCat http://postimg.org/image/jql10fsgz/ what am I doing wrong? Is `onProgressUpdate` printing on another Thread?How can I see it?Thank you again – Andoxko Dec 18 '13 at 12:33
  • do you have this line ` publishProgress(i++);` – A.S. Dec 18 '13 at 13:20
  • Yes I have it. I have copied your class into my project. it doesn't make any sense... – Andoxko Dec 19 '13 at 07:28