0

I have created a simple AsyncTask and I want to cancel the same if it takes more than 3 seconds. This is what I have done after following this post :

Async Caller:

execTask = new StartParseDownload();
            execTask.execute();
            //new StartParseDownload(this).execute();
            Handler handler = new Handler();
            handler.postDelayed(new Runnable()
            {
              @Override
              public void run() {
                  if ( execTask.getStatus() == AsyncTask.Status.RUNNING )
                      execTask.cancel(true);
              }
            }, 300 );

The task:

public class StartParseDownload extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;
        private boolean result = false;

        public StartParseDownload() {
        }

        protected void onPreExecute(){
        }

        @Override
        protected void onPostExecute(final Boolean success){

            super.onPostExecute(success);

        }

         @Override
            protected void onCancelled() {
                Log.e("Task cancelled", "Task cancelled");
            }

        protected Boolean doInBackground(final String... args){
            parObj.saveInBackground(new SaveCallback() {
                @Override
                public void done(com.parse.ParseException e){
                    if (e == null) {
                        result = true;
                        goIntoMainScreen();
                    } else {
                        result = false;
                    }

                    if (e != null) {
                    }
                }
            });
            return false;

        }
    }

Where am I going wrong? Why is the onCancelled() not getting called?

Community
  • 1
  • 1
kittu88
  • 2,451
  • 5
  • 40
  • 80

3 Answers3

0

1 .Your value is 300 , not 3000(3 Seconds)

2 .Also check like :

if ( execTask.getStatus() == AsyncTask.Status.RUNNING ){ boolean s = execTask.cancel(true); Log.e("Taskcancelled", "Task cancelled " +s); }else{ Log.e("Taskcancelled", "Task cancelled already"); }

May be your task already completed before call to cancel.

Satty
  • 1,352
  • 1
  • 12
  • 16
0

Inside your doinbackground you have to check if the AsyncTask gets cancelled. You call cancel but the AsyncTask will continue running if it's still in the dounbackground state. Then after it's done onCancel gets called. Perhaps in your case it doesn't get called after 3 seconds because your doinbacground method takes longer than 3 seconds...or quicker!

By the way I think you mean to run the Handler for 3000 not 300

mastrgamr
  • 631
  • 1
  • 11
  • 21
0

This answer might help.

The documentation states about the two variants:

Added in API level 11:

protected void onCancelled (Result result)

Added in API level 11 Runs on the UI thread after cancel(boolean) is invoked and doInBackground(Object[]) has finished.

The default implementation simply invokes onCancelled() and ignores the result. If you write your own implementation, do not call super.onCancelled(result).

Added in API level 3:

protected void onCancelled ()

Added in API level 3 Applications should preferably override onCancelled(Object). This method is invoked by the default implementation of onCancelled(Object).

Runs on the UI thread after cancel(boolean) is invoked and doInBackground(Object[]) has finished.

I haven't tested this, but you might want to try implementing both overrides:

   @Override
   protected void onCancelled() {
     Log.e("Task cancelled", "Task cancelled");
   }

   @Override
   protected void onCancelled(Boolean result) {
     Log.e("Task cancelled", "Task cancelled, result: " + result);
   }

It might also help to log the result of cancel() to make sure that you are successfully canceling the AsyncTask.

if ( execTask.getStatus() == AsyncTask.Status.RUNNING ){
        boolean cancelled = execTask.cancel(true);
        if (cancelled){
           Log.d("cancel", "Task cancelled");
        }
        else{
           Log.e("cancel", "Task not cancelled");
        }
}
Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137