Freewind
Freewind

Reputation: 198188

How to do something after all asynchronous tasks finished?

I'm using AsyncTask to download some files, and want to do something after all tasks finished.

Is there any easy way to do this?

Upvotes: 2

Views: 2066

Answers (3)

Ash
Ash

Reputation: 1451

You can use onPostExecute() callback when Asyn task finishes background processing, In a typical scenarion you would notify the UI (list adapter or UI Activity) that download of the File is finished and UI can refresh or populate the data.

onPreExecute(), invoked on the UI thread immediately after the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

Please have a look at this Android Ref example:

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));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

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

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

http://developer.android.com/reference/android/os/AsyncTask.html

Example2:

https://github.com/ashutoshchauhan13/TwitterFeedApp/blob/master/TwitterFeedApp/src/com/sixthsense/twitterfeed/ui/TwitterFeedActivity.java

Upvotes: 0

Jason Hessley
Jason Hessley

Reputation: 1628

Keep track of how many async tasks you have running and do something when the total is 0.

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    public int numOfTasks = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public void addTask(){
        numOfTasks++;
    }

    public void removeTask(){
        numOfTasks--;
    }

    public void allTasksComplete(){

        if(numOfTasks ==0){
            //do what you want to do if all tasks are finished
        }

    }

    class RequestTask extends AsyncTask<String, String, String>{

        @Override
        protected String doInBackground(String... uri) {

            String responseString = "";
            return responseString;
        }

         @Override
            protected void onPreExecute() 
            {
                    addTask(); // adds one to task count.
                    super.onPreExecute();

            }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            removeTask(); // subtracts one from task count.
            allTasksComplete(); // checks to see if all tasks are done...  task count is 0
            }
        }
    }

Upvotes: 4

Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24423

AsyncTask has a callback method name onPostExecute. It will be execute when the background task finish.

Upvotes: 0

Related Questions