AsyncTask does not call onProgressUpdate, onPostExecute

I am trying to understand usage AsyncTaskon Android and for this purpose I have developed this simple code.

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

Did I miss something?

Thanks at advancde

  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 everything works correctly except 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

+4
source share
2 answers

Method signature is incorrect. It should look like this:

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

In addition, I suggest you use annotation @Override. This way you will get a compile time error if you try to override the wrong method.

: onProgressUpdate, , . :

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

AsyncTask onProgressUpdate (Void...), , , , doInBackground(), publicProgress().

. docs

, :

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);
    }
}
+1

All Articles