0

I am working on AsyncTask. I am trying to stop the AsyncTask.

authenticationArrows mTask = new authenticationArrows(); 
mTask.execute();

I am trying to stop it by

mTask.cancel(true);

But it not stop.

My AsyncTask code is below.

class authenticationArrows extends AsyncTask<Void, Void, Boolean> 
{
    @Override
    protected void onCancelled() {
        super.onCancelled();

        cancel(true);

        // ask if user wants to try again
    }

     @Override
        protected Boolean doInBackground(Void... params) {
            return null;
        }
    protected void onPostExecute() {
    }

    @Override
    protected void onPreExecute() {

        if (!isCancelled()){
        handler.post(new Runnable() {

            @Override
            public void run() {
                centerImage.setVisibility(View.INVISIBLE);
                tiltMotion1.setVisibility(View.VISIBLE);                   

                progressStatus = 60;
                mProgressBar.setProgress(progressStatus);

                Vibrator v= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(200);
                progressStatus += 10;
                    mProgressBar.setProgress(progressStatus);                                                
            }
        });
        }
        if (!isCancelled()){
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                Motion1.setVisibility(View.INVISIBLE);
                Motion2.setVisibility(View.VISIBLE);
                Vibrator v= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(200);
                progressStatus += 10;
                mProgressBar.setProgress(progressStatus);
                startHandler();
            }
        }, 1000);
    }
    }

    public void startHandler(){
        if (!isCancelled()){
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                Motion2.setVisibility(View.INVISIBLE);
                Motion3.setVisibility(View.VISIBLE);
                Vibrator v= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(200);
                progressStatus += 10;
                mProgressBar.setProgress(progressStatus);
            }
        }, 1000);
        }
    }
}

How can I stop or cancel it when navigate to other activity or when App goes to background?

Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
user3555472
  • 836
  • 3
  • 11
  • 38

3 Answers3

1

The cancel(...) method prevents the call of onPostExecute() after doInBackground() finishes. Instead onCancelled() will be invoked. Furthermore isCancelled() will return true as soon as you call cancel(). You should check for this in your doInBackground(), to be able to let it finish ASAP.

So, this is useless code:

@Override
protected void onCancelled() {
    super.onCancelled();
    cancel(true);
}

cancel(true) has no effect here, because onCancelled() will only be invoked if you call cancel() before. You can leave it empty or deal with the situation here if your AsyncTask will be canceled.

Here you should check for isCancelled():

 @Override
 protected Boolean doInBackground(Void... params) {
     return null;
 }

Because you do nothing here your AsyncTask is useless, because you do no background threading at all.

This is the wrong place to call isCancelled():

@Override
protected void onPreExecute() {

    if (!isCancelled()){
        ...
    }

Again, isCancelled() should be checked in doInBackground() to let it finish ASAP.

To cancel your Task by switching Activities or an Activity goes to background, use the onPause() callback and call mTask.cancel(true); Read the docs about the AsyncTask carefully again to get the basics.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
0

The reason why things aren't stopping for you is because the process (doInBackground()) runs until it is finished. Therefore you should check if the thread is cancelled or not before doing stuff:

if(!isCancelled()){
// Do your stuff
}

So basically, if the thread is not cancelled, do it, otherwise skip it :) Could be useful to check for this some times during your operation, especially before time taking stuff.

Also it could be useful to "clean up" a little in

onCancelled();

By example:

protected Long doInBackground(URL... urls) {

     for (int i = 0; i < count; i++) {
      // you need to break your loop on particular condition here

         if(isCancelled())
              break;             
     }
     return totalSize;
 }

Hope this helps!

Juan Pedro Martinez
  • 1,924
  • 1
  • 15
  • 24
0

is your ProgressBar is set to cancellable false.

add action listener before mProgressBar.show() like this:

mProgressBar.setOnCancelListener(new DialogInterface.OnCancelListener(){
      public void onCancel(DialogInterface dialog) {
          mTask.cancel(true);
          //finish();
      }
});
Neha Shukla
  • 3,572
  • 5
  • 38
  • 69