AsyncTask not calling onProgressUpdate,onPostExecute

Viewed 4521

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

2 Answers
Related