-1

Hi and thanks for your attention,

I have the following code, which

  • starts an AsyncTask when a button is pressed

and

  • should stop the AsyncTask when the button is released.

But this does not happen...

(the AsyncTask continues until its natural end, when the MAXX value is reached)

Please what am I doing wrong?

class right extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... unused) {
            while (x < MAXX) {
                if (x < MAXX)
                    x++;
                publishProgress();
                SystemClock.sleep(40);
            }
            return (null);
        }

        @Override
        protected void onProgressUpdate(Void... unused) {
            horizontal.scrollTo(x, 0);
            Log.e("x", Integer.toString(x));
        }

        @Override
        protected void onPostExecute(Void unused) {

        }
    }



    left.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            right righttask = new right();
            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                righttask.execute();
                break;
            case MotionEvent.ACTION_UP:
                righttask.cancel(true);
                Log.e("","SHOULD STOP ASYNCTASK!!!");
                break;
            }
            return false;
        }
    });
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
  • if you read the documentation for `cancel`, you'll see that it does not do what you want it to. (i don't see the point of the if condition, by the way.) – njzk2 Nov 27 '13 at 14:34

4 Answers4

3

Add this to your while loop :

if (isCancelled()) break;

More informations : Usage of AsyncTask

2

Have a look at the Documentation

It is also defined that the doInBackGround() will not be canceled. Therefore you should check in your loop if the thread is canceled and break/leave it as mentioned in the example in the Documentation.

So you code should look loke this

        @Override
        protected Void doInBackground(Void... unused) {
            while (x < MAXX) {
                if (x < MAXX)
                    x++;
                publishProgress();
                if (isCancelled()) break;  //Add this line!
                SystemClock.sleep(40);
            }
            return (null);
        }

Sorry but I have to say a few more things:

  1. Classes should have names with upper Case starting.
  2. Why aren't you using a for loop when u want to increment x
  3. Variables should start with lower case

so usually your code should look like this:

class Right extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... unused) {
        for(int x = 0; x < MAXX; x++){

            publishProgress();
            if (isCancelled()) break;  //Add this line!            
            SystemClock.sleep(40);
        }
        return (null);
    }

    @Override
    protected void onProgressUpdate(Void... unused) {
        horizontal.scrollTo(x, 0);
        Log.e("x", Integer.toString(x));
    }

    @Override
    protected void onPostExecute(Void unused) {

    }
}


Right righttask;  //make it global!
left.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            righttask = new Right();
            righttask.execute();
            break;
        case MotionEvent.ACTION_UP:
            righttask.cancel(true);
            Log.e("","SHOULD STOP ASYNCTASK!!!");
            break;
        }
        return false;
    }
});
A.S.
  • 4,574
  • 3
  • 26
  • 43
1

I think this post could be useful, maybe your problem is the same : Android AsyncTask won't stop when cancelled, why?

Community
  • 1
  • 1
Pull
  • 2,236
  • 2
  • 16
  • 32
1

Hi there just left my working desk, and I do not have the IDE on my laptop so just writing a code out of mind, try to use a Runnable instead of a AsyncTask:

    //GLOABLS
boolean threadBool = false;
Handler handler;
    Thread thread;

onCreate(){
    handler = new Handler();


    left.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                threadBool = true;
                // handler.post(myRunabble);
                                    thread = newThread(myRunnable);
                                    thread.start();
                break;
            case MotionEvent.ACTION_UP:
                threadBool = false;
                break;
            }
            return false;
        }
    });
}

Runnable myRunnable = new Runnable() {
      @Override
      public void run() {
           for(int x = 0; x < MAXX; x++ ){
              handler.post(new Runnable(){
                  public void run() {
                     horizontal.scrollTo(x, 0);
              });
               if (threadBool == false){
                           threadBool = true;
                           break;
                       } 

               try{ Thrad.sleep(100); }catch(Exception e){} //catch the right one here
           }
      }
 };
A.S.
  • 4,574
  • 3
  • 26
  • 43