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
}
Author by
yital9
Updated on June 04, 2022Comments
-
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 about 12 yearsbut i have no object?? im right that each time AsyncTask create new objec? so where check for status??
-
yital9 about 12 yearsyou create new object befor execute, checking this object - it will never be running
-
yital9 about 12 yearsdoingAsyncTask is a reference object of your AsyncTask --- sory, what is it ?
-
yital9 about 12 yearsyou create new object befor execute, checking this object - it will never be running
-
yital9 about 12 yearsthis will not work too, AsyncTask need to create new object each execute
-
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 about 12 yearsthe main question is : AsyncTask have to create new object each execute, so check status of new object isnt helpful, u know
-
yital9 about 12 yearsthe same problem, AsyncTask need to create new object each execute
-
yital9 about 12 yearsits near, but not working ) look at my solution at the top, thx u!
-
yital9 about 12 yearsno, when you click second time , status will be finished and click do nothing, look at my solution, thx
-
yital9 about 12 yearsbut you write yourself : AsyncTask.Status PENDING Indicates that the task has not been executed yet. PENDING != RUNNING.. why wrong?
-
yital9 about 12 yearsafter execute, when finished ill get this status and the create new object
-
user370305 about 12 yearsI 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 about 12 yearsyou need asyncTask = new doingAsyncTask(); before asyncTask.execute(); cause if status will be finished it ll not working
-
user370305 about 12 yearsjust read my above comment carefully.. "make a new object in it."
-
Vishal Thakkar over 8 yearsHello 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 over 8 yearsDid 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!