无法取消运行AsyncTask

时间:2013-12-23 11:07:37

标签: android android-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);
    }
}

}

2 个答案:

答案 0 :(得分:0)

试试这个...... 删除变量.. cancelled并更改为此..

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

调用super.onCancelled代替..

并在doInBackground检查

if (isCancelled()) {
    break;
}

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

致电

希望它有效:)

答案 1 :(得分:0)

private YourAsyncTask ut;

在您的活动中声明您的asyncTask。

ut = new YourAsyncTask().execute();

像这样实例化。

ut.cancel(true);

像这样杀死/取消它。