Android, Asynctask, Check Status

Android, AsyncTask, check status?

getStatus() checks whether the the AsyncTask is pending, running, or finished.

LoadMusicInBackground lmib = new LoadMusicInBackground();

if(lmib.getStatus() == AsyncTask.Status.PENDING){
// My AsyncTask has not started yet
}

if(lmib.getStatus() == AsyncTask.Status.RUNNING){
// My AsyncTask is currently doing work in doInBackground()
}

if(lmib.getStatus() == AsyncTask.Status.FINISHED){
// My AsyncTask is done and onPostExecute was called
}

If you want to check if your action actually succeeded (i.e. the music was successfully loaded), then you need to come up with your own method that determines that. The getStatus() can only determine the state of the actual thread in AsyncTask.

How to check if Async Task is already running

I think you should check the concept of Application in Android.
http://developer.android.com/reference/android/app/Application.html

In fact there is no such thing as

different instance of the app

. The Application is always the same for all your Activities/Services.
That means that you'd left the Activity and opened it again, 2 cases are possible:

  1. The system already killed your application. In this case AsyncTask is dead already and it's safe to start a new one
  2. The Application was still alive, so AsyncTask possibly still running.

In 2nd case I will recommend to use some static variables, pointing to this AsyncTask or it's state. If your app was still alive when 2nd time opened - all static references will be still valid, so you can successfully operate.

PS: By the way, in current approach be aware that your application can be terminated by the system at any time. So AsyncTask can be interrupted in any moment. It it's not ok for you - please check IntentServices - components, specially designed for background-operation purpose. http://developer.android.com/reference/android/app/IntentService.html

Good luck!

Android : Get status of Asynctask and wait

I have an idea to make async series in just one async task:

protected Boolean doInBackground(String... params) {
if(params[0] == "taskA") {
//do somthing
params[0] = "taskB";
}
if(params[0] == "taskB") {
//do somthing
params[0] = "taskC";
}
if(params[0] == "taskC") {
//do somthing
params[0] = "taskD";
}
if(params[0] == "taskD") {
//do somthing
return true;
}

And in your main thread just call async task like this:

ShowMyProgress();
new MyAsyncTask().execute("taskA");

And finally you can hide your progress on onPostExecute like:

protected void onPostExecute(final Boolean success) {
if (success) {
....

HideMyProgress();
}
}

How to check the status of AsyncTask in the following code?

Use get()

SaveDataOffline saveDataOffline = new SaveDataOffline(getApplicationContext());
saveDataOffline.execute().get();
AsyncTaskStopWords asyncTaskStopWords = new AsyncTaskStopWords(getBaseContext());
asyncTaskStopWords.execute().get();

Code will wait until AsyncTask finishes before going to the next line.

AsyncTask status is always running

Hello you can force close the AsyncTask
Please use the following code in yout AsyncTask with condition and call it in your activity where you want to stop, it will stop your asynk task running

/**
* Check if asynctask is running, if still running cancel it.
*/
public void forceCancel(){
if (getStatus().equals(AsyncTask.Status.RUNNING)) {
cancel(true);
}
}

Check for working AsyncTask

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
}
}
// ...
}


Related Topics



Leave a reply



Submit