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:
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){}