为什么AsyncTask不会停止?

时间:2013-11-27 14:33:08

标签: android

嗨,谢谢你的关注,

我有以下代码,

  • 按下按钮时启动AsyncTask

  • 应该在释放按钮时停止AsyncTask。

但这不会发生......

(当达到MAXX值时,AsyncTask一直持续到自然结束)

请问我做错了什么?

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;
        }
    });

4 个答案:

答案 0 :(得分:3)

将此添加到您的while循环中:

if (isCancelled()) break;

更多信息:Usage of AsyncTask

答案 1 :(得分:2)

查看Documentation

还定义了doInBackGround()不会被取消。因此,如果线程被取消,您应该检查您的循环,并按照文档中的示例中的说明进行中断/保留。

所以你的代码应该看起来这个

        @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);
        }

很抱歉,我还要说几件事:

  1. 类应具有大写开头的名称。
  2. 当你想增加x
  3. 时,为什么不使用for循环?
  4. 变量应以小写字母开头
  5. 所以通常你的代码应该是这样的:

    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;
        }
    });
    

答案 2 :(得分:1)

我认为这篇文章可能有用,也许你的问题是一样的: Android AsyncTask won't stop when cancelled, why?

答案 3 :(得分:1)

嗨,刚离开我的办公桌,我的笔记本电脑上没有IDE,所以只记得编写代码,尝试使用Runnable而不是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
           }
      }
 };