You can retreive the return value of protected Boolean doInBackground()
by calling the get() method of AsyncTask
class :
AsyncTaskClassName task = new AsyncTaskClassName();
bool result = task.execute(param1,param2......).get();
But be careful of the responsiveness of the UI, because get()
waits for the computation to complete and will block the UI thread.
If you are using an inner class, it's better to do the job into the onPostExecute(Boolean result) method.
If you just want to update the UI, AsyncTask
offers you two posibilites :
To update the UI in parallel with the task executed in doInBackground()
(e.g. to update a ProgressBar
), you'll have to call publishProgress()
inside the doInBackground()
method. Then you have to update the UI in the onProgressUpdate()
method.
To update the UI when the task is done, you have to do it in the onPostExecute()
method.
/** This method runs on a background thread (not on the UI thread) */
@Override
protected String doInBackground(String... params) {
for (int progressValue = 0; progressValue < 100; progressValue++) {
publishProgress(progressValue);
}
}
/** This method runs on the UI thread */
@Override
protected void onProgressUpdate(Integer... progressValue) {
// TODO Update your ProgressBar here
}
/**
* Called after doInBackground() method
* This method runs on the UI thread
*/
@Override
protected void onPostExecute(Boolean result) {
// TODO Update the UI thread with the final result
}
This way you don't have to care about responsiveness problems.