2013-12-23 140 views
1

我在AsyncTask子类中遇到了一些麻烦。无法取消运行AsyncTask

我有一个主要的活动,如下所示,显示一个按钮和一个数字在屏幕上计数。点击按钮启动一个可以输入数字的编辑活动。

主活动上显示的数字应该更新与它的计时器,但我遇到的麻烦是我无法停止计时器。它应该在进入Edit活动并且从它返回时停止,并且也可以使用新值重新启动,但它不会,定时器始终以第一个输入的值运行,即使当我离开时它也不会停止该程序并返回到主屏幕。

我看过这里的帖子,如Can't cancel Async task in android,但他们都只是提到检查isCancelled(),我正在做。任何人都可以看到/解释为什么我不能阻止这个AsyncTask?

public class MainActivity extends Activity { 

@Override 
UpdateTimer ut; 
TextView tvn; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

@Override 
public void onResume() { 
    super.onResume(); 

    tvn = (TextView) findViewById(R.id.numDisplay); 

    if(ut != null)//&& ut.getStatus() == AsyncTask.Status.RUNNING) { 
     ut.cancel(true); 
     ut.cancelled = true; 
     Log.d("-----M_r","called cancel: "+ut.isCancelled()+" "+cancelled); 
    } 

    if (updateRequired) { 
     ut = new UpdateTimer(); 
     ut.execute(number); 
     updateRequired = false; 
    } 
} 

public void onEditButtonPressed(View caller) { 

    // kill any running timer 
    if(ut != null) 
    { 
     ut.cancel(true); 
     ut.cancelled = true; 
    } 

    // start the edit screen 
    Intent e_intent = new Intent(this, EditActivity.class); 
    startActivity(e_intent); 
} 

private void updateScreen(long number) { 

    // update screen with current values 
    tvn.setText("" + number); 
} 

private class UpdateTimer extends AsyncTask<Long, Long, Integer> { 

    long number; 
    public boolean cancelled; 

    @Override 
    protected Integer doInBackground(Long... params) { 

     number = params[0]; 
     cancelled = false; 

     while(true) { 
      number += 1; 

      //sleep for 1 second 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      // tell this AsyncTask to update the time on screen 
      publishProgress(number); 

      // check if timer needs to stop 
      if (isCancelled()) break; 
      if(cancelled) break; 
     } 

     return null; 
    } 

    protected void onProgressUpdate(Long... progress) { 
      Log.d("-----M_ut","updated: "+number+" "+this.isCancelled()+" "+cancelled); 
     updateScreen(progress[0]); 
    } 

    protected void onCancelled(Integer result) { 
     cancelled = true; 
     Log.d("-----M_ut","-- cancelled called: "+this.isCancelled()); 
    } 

} 

protected void onStop() 
{ 
    super.onStop(); 

    // kill any running timer 
    if(ut != null) { 
     ut.cancel(true); 
    } 
} 

}

回答

0

尝试...... 删除变量.. cancelled并切换到这个..

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

呼叫super.onCancelled而不是..

而在doInBackground检查

if (isCancelled()) { 
    break; 
} 

尝试从您的活动呼吁ut.cancel(true);

希望工程:)

+0

感谢您的建议,但它没有奏效。 我之前忘记提到,关于isCancelled的日志行全部返回true,除了onProgressUpdate中的日志行。 –

0
private YourAsyncTask ut; 

声明你的AsyncTask您的活动。

ut = new YourAsyncTask().execute(); 

初始化它这样。

ut.cancel(true); 

杀/取消像这样。

+0

谢谢,但这没有奏效。 我看不到为什么isCancelled从除了doInBackground和onProgressUpdate以外的其他地方返回true。 –