64

When calling AsyncTask<Integer,Integer,Boolean>, where is the return value of:

protected Boolean doInBackground(Integer... params)?

Usually we start AsyncTask with new AsyncTaskClassName().execute(param1,param2......); but it doesn't appear to return a value.

Where can the return value of doInBackground() be found?

CaptJak
  • 3,592
  • 1
  • 29
  • 50
Dhruv
  • 1,129
  • 2
  • 13
  • 32

2 Answers2

67

The value is then available in onPostExecute which you may want to override in order to work with the result.

Here is example code snippet from Google's docs:

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
      protected Long doInBackground(URL... urls) {
          int count = urls.length;
          long totalSize = 0;
          for (int i = 0; i < count; i++) {
              totalSize += Downloader.downloadFile(urls[i]);
              publishProgress((int) ((i / (float) count) * 100));
          }
          return totalSize;
      }

      protected void onProgressUpdate(Integer... progress) {
          setProgressPercent(progress[0]);
      }

      protected void onPostExecute(Long result) {
          showDialog("Downloaded " + result + " bytes");
      }
 }
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
  • 9
    To make this of any real use other than logging or showing a dialog, you can nest the class you extend `AsyncTask` with in your UI (or other) class and then call a method from that class with the return value in the `onPostExecute` method. – cjk Sep 14 '11 at 21:17
36

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.

  • Cyril Leroux
    • 2,599
    • 1
    • 26
    • 25
    • 3
      this is nor right way .get() will freeze the UI can you sugges any other option i wan to use return result of async task then want to exucute other web servecice pls help me – CoronaPintu May 27 '13 at 05:34
    • 3
      If you read my post beyond the fifth line, you will see that I recommend the use of `OnPostExecute()` rather than the `get()` to avoid blocking the UI thread. – Cyril Leroux May 27 '13 at 07:10
    • ya but i want to use return value and on the base of return value i want to call multiple url then what can i do pls help me – CoronaPintu May 27 '13 at 07:31
    • 1
      Then you have to define a AsyncTask that returns the type you want. Probably a String instead of a boolean : `private class MyAsyncTask extends AsyncTask` (the last parameter define the result type). Then you can deduce and call your urls from onPostExecute(String result). – Cyril Leroux May 27 '13 at 08:02
    • very helpful. Thanks! – narb Apr 10 '17 at 17:22
    • if you are going to pause the current thread, why would you even use `AsyncTask` in first place? – Aras Feb 14 '19 at 22:15