Check for working AsyncTask
android, android-asynctask, multithreading
Solution
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
}
}
// ...
}
Problem
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(); } ```