Check for working AsyncTask

14,397

Solution 1

Check this AsyncTask.Status

AsyncTask.Status    FINISHED    Indicates that onPostExecute(Result) has finished. 
AsyncTask.Status    PENDING     Indicates that the task has not been executed yet. 
AsyncTask.Status    RUNNING     Indicates that the task is running. 

code:

if (doingAsyncTask().getStatus().equals(AsyncTask.Status.FINISHED))
     doingAsyncTask().execute();
else

EDIT:

public class SearchActivity extends Activity {
    doingAsyncTask asyncTask; 

    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);             
       // ...
       asyncTask = new doingAsyncTask();   
    }
    public void onclickButton(View view) {
         if(ayncTask.getStatus().equals(AsyncTask.Status.FINISHED) || ayncTask.getStatus().equals(AsyncTask.Status.PENDING)) {
             asyncTask.execute();
         }
         else {
             // do something
         }
    }
    // ...
}

Solution 2

You can check the status of AsyncTask => AsyncTask.Status

For example:

    myAsyncTask mtask = new myAsyncTask();
    mtask.execute();

   // write this wherever you want to check status
   if(mtask.getStatus() == AsyncTask.Status.FINISHED){
        // My AsyncTask is done and onPostExecute was called
    }
Share:
14,397
yital9
Author by

yital9

Updated on June 04, 2022

Comments

  • yital9
    yital9 about 2 years

    I have some work to do in the another thread (by doingAsyncTask). Work start when user click on button. But at the same time only one object of doingAsyncTask must do this work, i meen if doingAsyncTask is working, then click on button must not create a new object of doingAsyncTask and execute it, it must wait until work finish. How can i check it?

    public class SearchActivity extends Activity {
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //....
        }
    
        public void onclickButton(View view) {
            new doingAsyncTask().execute();
        }
    
        public class doingAsyncTask extends AsyncTask<Void, Void, Void> {
        protected Void doInBackground(Void... unused) {
            //doing something
            return(null);
        }
    
        protected void onProgressUpdate() {
        }
        protected void onPreExecute() {
        }
        protected void onPostExecute() {
        }
       }
    }
    

    SOLVED thx all , its works for me

          if(task.getStatus() == AsyncTask.Status.FINISHED)
                task=new ProgressBarShow();
            if(task.getStatus() == AsyncTask.Status.PENDING){
                //task=new ProgressBarShow();
                task.execute();
            }
    
  • yital9
    yital9 about 12 years
    but i have no object?? im right that each time AsyncTask create new objec? so where check for status??
  • yital9
    yital9 about 12 years
    you create new object befor execute, checking this object - it will never be running
  • yital9
    yital9 about 12 years
    doingAsyncTask is a reference object of your AsyncTask --- sory, what is it ?
  • yital9
    yital9 about 12 years
    you create new object befor execute, checking this object - it will never be running
  • yital9
    yital9 about 12 years
    this will not work too, AsyncTask need to create new object each execute
  • Paresh Mayani
    Paresh Mayani about 12 years
    // it was just for idea. If i give object name then again a question will raise that what is mtask actually? thats why i have written that line.
  • yital9
    yital9 about 12 years
    the main question is : AsyncTask have to create new object each execute, so check status of new object isnt helpful, u know
  • yital9
    yital9 about 12 years
    the same problem, AsyncTask need to create new object each execute
  • yital9
    yital9 about 12 years
    its near, but not working ) look at my solution at the top, thx u!
  • yital9
    yital9 about 12 years
    no, when you click second time , status will be finished and click do nothing, look at my solution, thx
  • yital9
    yital9 about 12 years
    but you write yourself : AsyncTask.Status PENDING Indicates that the task has not been executed yet. PENDING != RUNNING.. why wrong?
  • yital9
    yital9 about 12 years
    after execute, when finished ill get this status and the create new object
  • user370305
    user370305 about 12 years
    I combine both condition in one, Now you can use the only one if condition. and also make a new object in it. Look at my answer.
  • yital9
    yital9 about 12 years
    you need asyncTask = new doingAsyncTask(); before asyncTask.execute(); cause if status will be finished it ll not working
  • user370305
    user370305 about 12 years
    just read my above comment carefully.. "make a new object in it."
  • Vishal Thakkar
    Vishal Thakkar over 8 years
    Hello sir, I want to call 3 asynctask in main activity simultaneously.i was given this solution but it not working.just first asynctask call then another inside condition not called..dont know how to solve this problem ?
  • Paresh Mayani
    Paresh Mayani over 8 years
    Did you checked why next asynctask is not getting called? Why that condition is not getting called? I can't help without seeing what you are doing exactly but Debug can help you for sure!