Reputation: 6712
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();
}
Upvotes: 4
Views: 14419
Reputation: 109247
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
}
}
// ...
}
Upvotes: 9
Reputation: 20348
The code could be like this:
doingAsyncTask task;
public void onclickButton(View view) {
if(task==null){
task=new doingAsyncTask();
task.execute();
}
else if(task.Status!=RUNNING){
task=new doingAsyncTask();
task.execute();
}
}
Upvotes: 0
Reputation: 128428
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
}
Upvotes: 2
Reputation: 68187
Use getStatus() of AsyncTask to check its status before setting a new task
Upvotes: 0