I have implemented AsyncTask in my one of activity:

 performBackgroundTask asyncTask = new performBackgroundTask();
 asyncTask.execute();

Now, i need to implement the "Cancel" button functionality, so i have to stop the execution of the running task. I don't know how do i stop the running task(background task).

So Please suggest me, how do i cancel the AsyncTask forcefully ?

Update:

I found about the Cancel() method of the same, but i found that calling cancel(boolean mayInterruptIfRunning) doesn't necessarily stop the execution of the background process. All that seems to happen is that the AsyncTask will execute onCancelled(), and won't run onPostExecute() when it completes.

Solution 1

Just check isCancelled() once in a while:

 protected Object doInBackground(Object... x) {
    while (/* condition */) {
      // work...
      if (isCancelled()) break;
    }
    return null;
 }

Solution 2

Call cancel() on the AsyncTask. Whether or not this will actually cancel anything is dependent a bit upon what you are doing. To quote Romain Guy:

If you call cancel(true), an interrupt will be sent to the background thread, which may help interruptible tasks. Otherwise, you should simply make sure to check isCancelled() regularly in your doInBackground() method. You can see examples of this at code.google.com/p/shelves.

Solution 3

It really depends on what you are doing in your asynctask.

If it's a loop processing a lot of files, you can just check after each files if the isCanceled() flag is raised or not and then break from your loop if it is.

If it's a one line command that performs a very long operation, there's not much you can do.

The best workaround would be to not use the cancel method of the asynctask and use your own cancelFlag boolean. You can then test this cancelFlag in your postExecute to decide what to do with the result.

Solution 4

The mentioned in comments case that isCancelled() always returns false even i call asynctask.cancel(true); is especially harmful if I close my app, but the AsyncTask continues working.

To solve this I modified the proposed by Jacob Nordfalk code in the following way:

protected Object doInBackground(Object... x) {
    while (/* condition */) {
      // work...
      if (isCancelled() || (FlagCancelled == true)) break;
    }
    return null;
 }

and added the following to the main activity:

@Override
protected void onStop() {
    FlagCancelled = true;
    super.onStop();
}

As my AsyncTask was a private class of one of views, so getters or setters of the flag were necessary to inform the AsyncTask about the currently actual flag value.

My multiple tests (AVD Android 4.2.2, Api 17) have shown that if an AsyncTask is already executing its doInBackground, then isCancelled() reacts in no way (i.e. continues to be false) to any attempts to cancel it, e.g. during mViewGroup.removeAllViews(); or during an OnDestroy of the MainActivity, each of which leads to detaching of views

   @Override 
   protected  void  onDetachedFromWindow() { 
    mAsyncTask.cancel(false); // and the same result with mAsyncTask.cancel(true);
    super.onDetachedFromWindow(); 
   } 

If I manage to force stopping the doInBackground() thanks to the introduced FlagCancelled, then onPostExecute() is called, but neither onCancelled() nor onCancelled(Void result) (since API level 11) are not invoked. (I have no idea why, cause they should be invoked and onPostExecute() should not, "Android API doc says:Calling the cancel() method guarantees that onPostExecute(Object) is never invoked." - IdleSun, answering a similar question).

On the other hand, if the same AsyncTask hadn't started its doInBackground() before cancelling, then everything is ok, isCancelled() changes to true and I may check that in

@Override
    protected void onCancelled() {
        Log.d(TAG, String.format("mAsyncTask - onCancelled: isCancelled = %b, FlagCancelled = %b", this.isCancelled(), FlagCancelled ));
    super.onCancelled();
}

Solution 5

Even though an AsyncTask should not be used for long running operations, sometimes it may be caught in a task that does not respond (such as a non-responding HTTP call). In that case, it may be necessary to cancel the AsyncTask.

We have to challenges in doing this. 1. The usual progress dialog displayed with an AsyncTask is the first thing cancelled on an AsyncTask when the back button is pressed by the user. 2. The AsyncTask may be in the doInBackground method

By creating a dismissDialogListerner on the ProgressDialog, a user can press the back button and actually nullify the AsycnTask and close the dialog itself.

Here is an example:

public void openMainLobbyDoor(String username, String password){
    if(mOpenDoorAsyncTask == null){
        mOpenDoorAsyncTask = (OpenMainDoor) new OpenMainDoor(username, password, Posts.API_URL, 
                mContext, "Please wait while I unlock the front door for you!").execute(null, null, null);
    }
}

private class OpenMainDoor extends AsyncTask<Void, Void, Void>{

    //declare needed variables
    String username, password, url, loadingMessage;
    int userValidated;
    boolean canConfigure;
    Context context;
    ProgressDialog progressDialog;

    public OpenMainDoor(String username, String password, String url, 
                Context context, String loadingMessage){
        userValidated = 0;
        this.username = username;
        this.password = password;
        this.url = url;
        this.context = context;
        this.loadingMessage = loadingMessage;
    }

    /**
     * used to cancel dialog on configuration changes
     * @param canConfigure
     */
    public void canConfigureDialog(boolean canConfigure){
        this.canConfigure = canConfigure;
    }

    @Override
    protected void onPreExecute(){
        progressDialog = new ProgressDialog(this.context);
        progressDialog.setMessage(loadingMessage);
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(true);
        progressDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                mOpenDoorAsyncTask.cancel(true);
            }
        });
        progressDialog.show();
        this.canConfigure = true;
    }

    @Override
    protected Void doInBackground(Void... params) {
        userValidated = Posts.authenticateNTLMUserLogin(username, password, url, context);
        while(userValidated == 0){
            if(isCancelled()){
                break;
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void unused){
        //determine if this is still attached to window
        if(canConfigure)
            progressDialog.dismiss();

        if(userValidated == 1){
            saveLoginValues(username, password, true);
            Toast.makeText(context, R.string.main_login_pass, Toast.LENGTH_SHORT).show();
        }else{
            saveLoginValues(username, password, false);
            Toast.makeText(context, R.string.main_login_fail, Toast.LENGTH_SHORT).show();
        }
        nullifyAsyncTask();
    }

    @Override
    protected void onCancelled(){
        Toast.makeText(context, "Open door request cancelled!", Toast.LENGTH_SHORT).show();
        nullifyAsyncTask();
    }
}

Solution 6

Our global AsyncTask class variable

LongOperation LongOperationOdeme = new LongOperation();

And KEYCODE_BACK action which interrupt AsyncTask

   @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            LongOperationOdeme.cancel(true);
        }
        return super.onKeyDown(keyCode, event);
    }

It works for me.