如何从 onPreExecute() 方法取消 ProcessDialog

How to cancel ProcessDialog from onPreExecute() method

当某些情况发生时,我不知道如何从 onPreExecute() return。 例如:

@Override
    protected void onPreExecute() {
       if(varCancel){
         //Do not execute doInBackground() method
        }
        super.onPreExecute();
    }

问题是,我想在方法 doInBackground() 开始之前取消这个过程。

使用AsyncTask.cancel(boolean mayInterruptIfRunning)取消任务的执行。

   if(varCancel){
     //Do not execute doInBackground() method
       this.cancel(true);
    }

或者使用class调用execute()启动AsyncTask的对象来取消它:

   if(varCancel){
     //Do not execute doInBackground() method
       <CLASS_OBJECT>.cancel(true);
    }