Answers:
才发现,原来AlertDialogs的boolean cancel(...);我已经用实际处处什么也不做。大。
所以...  
public class MyTask extends AsyncTask<Void, Void, Void> {
    private volatile boolean running = true;
    private final ProgressDialog progressDialog;
    public MyTask(Context ctx) {
        progressDialog = gimmeOne(ctx);
        progressDialog.setCancelable(true);
        progressDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                // actually could set running = false; right here, but I'll
                // stick to contract.
                cancel(true);
            }
        });
    }
    @Override
    protected void onPreExecute() {
        progressDialog.show();
    }
    @Override
    protected void onCancelled() {
        running = false;
    }
    @Override
    protected Void doInBackground(Void... params) {
        while (running) {
            // does the hard work
        }
        return null;
    }
    // ...
}while(running)用while(!isCancelled())正如其他人在这里的评论说。
                    如果您正在进行计算:
isCancelled()定期检查。如果您正在执行HTTP请求:
HttpGet或HttpPost某处(例如公共场所)。cancel,致电request.abort()。这将导致IOException您的计算机内部被抛出doInBackground。就我而言,我有一个用于各种AsyncTasks的连接器类。为简单起见,我abortAllRequests在该类中添加了一个新方法,并在调用之后直接调用了该方法cancel。
HttpGet.abort()从后台线程进行调用,否则会收到android.os.NetworkOnMainThreadException。
                    cancel(true)中断请求?来自文档:If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.
                    HttpURLConnection.disconnect();
                    cancel(true)。我用它,它的工作原理。
                    关键是AsyncTask.cancel()调用仅调用任务中的onCancel函数。这是您要处理取消请求的地方。
这是我用来触发更新方法的小任务
private class UpdateTask extends AsyncTask<Void, Void, Void> {
        private boolean running = true;
        @Override
        protected void onCancelled() {
            running = false;
        }
        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            onUpdate();
        }
        @Override
        protected Void doInBackground(Void... params) {
             while(running) {
                 publishProgress();
             }
             return null;
        }
     }running标志。AsyncTask具有一个内部标志,该标志在取消任务时设置。替换while (running)为while (!isCancelled())。  developer.android.com/reference/android/os/…   因此,在这种简单情况下,您不需要onCancelled()覆盖。
                    简单:请勿使用AsyncTask。AsyncTask专为快速结束(数十秒)的短操作而设计,因此不需要取消。“音频文件播放”不符合条件。您甚至不需要后台线程即可播放普通音频文件。
onPostExecute()以查看是否应该继续进行工作。
                    这就是我编写AsyncTask 
的方式,关键是添加Thread.sleep(1);   
@Override   protected Integer doInBackground(String... params) {
        Log.d(TAG, PRE + "url:" + params[0]);
        Log.d(TAG, PRE + "file name:" + params[1]);
        downloadPath = params[1];
        int returnCode = SUCCESS;
        FileOutputStream fos = null;
        try {
            URL url = new URL(params[0]);
            File file = new File(params[1]);
            fos = new FileOutputStream(file);
            long startTime = System.currentTimeMillis();
            URLConnection ucon = url.openConnection();
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            byte[] data = new byte[10240]; 
            int nFinishSize = 0;
            while( bis.read(data, 0, 10240) != -1){
                fos.write(data, 0, 10240);
                nFinishSize += 10240;
                **Thread.sleep( 1 ); // this make cancel method work**
                this.publishProgress(nFinishSize);
            }              
            data = null;    
            Log.d(TAG, "download ready in"
                  + ((System.currentTimeMillis() - startTime) / 1000)
                  + " sec");
        } catch (IOException e) {
                Log.d(TAG, PRE + "Error: " + e);
                returnCode = FAIL;
        } catch (Exception e){
                 e.printStackTrace();           
        } finally{
            try {
                if(fos != null)
                    fos.close();
            } catch (IOException e) {
                Log.d(TAG, PRE + "Error: " + e);
                e.printStackTrace();
            }
        }
        return returnCode;
    }我们的全局AsyncTask类变量
LongOperation LongOperationOdeme = new LongOperation();和KEYCODE_BACK动作会中断AsyncTask
   @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            LongOperationOdeme.cancel(true);
        }
        return super.onKeyDown(keyCode, event);
    }这个对我有用。
我不想强迫cancel(true)不必要地中断异步任务,因为它们可能有释放的资源,例如关闭套接字或文件流,将数据写入本地数据库等。另一方面,我遇到了这样的情况:异步任务拒绝部分时间自己完成任务,例如有时在关闭主活动时,我要求从活动onPause()方法内部完成异步任务。因此,这不是简单地调用的问题running = false。我必须选择一种混合解决方案:都调用running = false,然后给异步任务几毫秒以完成,然后调用cancel(false)或cancel(true)。
if (backgroundTask != null) {
    backgroundTask.requestTermination();
    try {
        Thread.sleep((int)(0.5 * 1000));
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    if (backgroundTask.getStatus() != AsyncTask.Status.FINISHED) {
        backgroundTask.cancel(false);
    }
    backgroundTask = null;
}附带的结果是,doInBackground()完成后有时会onCancelled()调用方法,有时会调用onPostExecute()。但是至少可以保证异步任务终止。
关于Yanchenko在10月4月29日的回答:使用'while(running)'方法很巧妙,因为在每次执行AsyncTask期间必须多次执行'doInBackground'下的代码。如果您每次执行AsyncTask时在“ doInBackground”下的代码仅需执行一次,则在“ AsyncTask本身被取消,因为只有在while循环中的所有代码至少执行一次之后,才会评估“ while(running)”条件。因此,您应该(a。)将“ doInBackground”下的代码分解为多个“ while(运行)”块,或者(b。)执行多个“ isCancelled”https://developer.android.com/reference/android/os/AsyncTask.html。
对于选项(a。),可以这样修改Yanchenko的答案,如下所示:
public class MyTask extends AsyncTask<Void, Void, Void> {
private volatile boolean running = true;
//...
@Override
protected void onCancelled() {
    running = false;
}
@Override
protected Void doInBackground(Void... params) {
    // does the hard work
    while (running) {
        // part 1 of the hard work
    }
    while (running) {
        // part 2 of the hard work
    }
    // ...
    while (running) {
        // part x of the hard work
    }
    return null;
}
// ...对于选项(b。),您在'doInBackground'中的代码将如下所示:
public class MyTask extends AsyncTask<Void, Void, Void> {
//...
@Override
protected Void doInBackground(Void... params) {
    // part 1 of the hard work
    // ...
    if (isCancelled()) {return null;}
    // part 2 of the hard work
    // ...
    if (isCancelled()) {return null;}
    // ...
    // part x of the hard work
    // ...
    if (isCancelled()) {return null;}
}
// ...