Android 如何停止异步任务的重复?

Android 如何停止异步任务的重复?,android,android-asynctask,Android,Android Asynctask,我正在使用异步任务,如下所示: private first_async first; // global variable // In onCreate first = new first_async(); first.execute(1); // then on click of the button I want to stop this, but it works after some time like this: if (!first.isCancelled()) {

我正在使用异步任务,如下所示:

private first_async first;  // global variable

// In onCreate
first = new first_async();
first.execute(1);

// then on click of the button I want to stop this, but it works after some time like this:
 if (!first.isCancelled()) {
     first.cancel(false);
 }

private class first_async extends AsyncTask<Integer, Void, Void> {

    int var;
    @Override
    protected void doInBackground(Integer... params) {
        while (!isCancelled()) {
           var = params[0];
           // do some task
         }  
        return null;
    }

    @Override
    protected void onPostExecute(void thumb) {
        super.onPostExecute(thumb);
        if (var >= 7) {
        } else {
            first_time_imageURL = new first_time_get_images_URL();
            first_time_imageURL.execute(var+1);
        }
    }
}
private first\u async first;//全局变量
//立即创建
first=新的first_async();
第一,执行(1);
//然后单击按钮,我想停止此操作,但过一段时间后它会像这样工作:
如果(!first.isCancelled()){
第一,取消(假);
}
私有类优先\u异步扩展异步任务{
int-var;
@凌驾
受保护的void doInBackground(整数…参数){
而(!isCancelled()){
var=params[0];
//做一些工作
}  
返回null;
}
@凌驾
受保护的void onPostExecute(void thumb){
super.onPostExecute(thumb);
如果(var>=7){
}否则{
first_time_imageURL=新的第一次获取图像_URL();
第一次执行(var+1);
}
}
}
因此,问题是,异步任务上的取消在一段时间后才能工作。如果我不正确,请纠正我,或者如果有其他选择,请提及

 if (!first.isCancelled()) {
 first.cancel(false);
}
False在这里应该为true,这将取消异步任务。因此:

first.cancel(true);