doAsyncTask 的 doInBackground 中的 Stop 方法

发布于 2024-12-05 06:19:01 字数 766 浏览 0 评论 0原文

我有这个 AsyncTask:

private class GetMyFlights extends AsyncTask<String, Void, Integer> {       
    private ProgressDialog dialog;  

    public GetMyFlights(ListActivity activity) {}           

    @Override
    protected Integer doInBackground(String... params) {
        return getData();
    }       

    @Override
    protected void onPostExecute(Integer result) {  
        super.onPostExecute(result);    
        //...
    }
}

当我更改为另一个活动时,我想停止它。所以我设置了这个:

@Override
protected void onPause() {
    if(mGetMyFlights != null){
        mGetMyFlights.cancel(true);
        Log.d(TAG, "MyFlights onPause, cancel task");
    }
    super.onPause();
}

但是当我更改活动时,getData 中的代码仍然有效。我如何确定已停止?

I have this AsyncTask:

private class GetMyFlights extends AsyncTask<String, Void, Integer> {       
    private ProgressDialog dialog;  

    public GetMyFlights(ListActivity activity) {}           

    @Override
    protected Integer doInBackground(String... params) {
        return getData();
    }       

    @Override
    protected void onPostExecute(Integer result) {  
        super.onPostExecute(result);    
        //...
    }
}

When I change to another activity I want to stop it. So I set this:

@Override
protected void onPause() {
    if(mGetMyFlights != null){
        mGetMyFlights.cancel(true);
        Log.d(TAG, "MyFlights onPause, cancel task");
    }
    super.onPause();
}

But the code inside getData is still working when I change my activity. How can I be sure is stop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风念你 2024-12-12 06:19:01

从我读到的所有内容来看,cancel() 方法似乎不是停止 AsyncTask 的可靠方法。中断被发送到后台线程,但这仅对可中断任务有效。普遍的共识是,为了确保 AsynTask 停止,您应该在 AsyncTask 的 doInBackground 方法中不断检查 isCancelled() 。

From everything I've read, it seems that the cancel() method is not a reliable way to stop an AsyncTask. An interrupt is sent to the background thread, but this is only effective on interruptable tasks. The general consensus is that, to ensure the AsynTask is stopped, you should continually check isCancelled() within the doInBackground method of your AsyncTask.

叹梦 2024-12-12 06:19:01

我通常做的是在 AsyncTask 实例上调用 cancel(true) 并检查 doInBackground 中的Thread.interrupted()。您可以检查 isCancelled(),但如果实际工作是在独立于您的 AsyncTask 并且不知道它的其他类中完成的,那么这不起作用。例如(直接从我自己的 Data() 方法复制到我的 Data 类中,独立于任何活动、异步任务等):

while ((line = reader.readLine()) != null) {
    if (Thread.interrupted()) throw new InterruptedException();
    content.append(line);
}

只需确保处理doInBackground() 中的 InterruptedException,例如:

@Override
protected Integer doInBackground(String... params) {
    try {
        return getData();
    }
    catch (InterruptedException e) {
        Log.d("MyApp", "Girl, Interrupted");
        return -1;
    }
}

还值得注意的是,如果任务不会调用 onPostExecute()被取消。相反,onCancelled() 被调用。

What I usually do is call cancel(true) on the AsyncTask instance and check Thread.interrupted() in doInBackground. You could check isCancelled(), but that doesn't work if the actual work is done in some other class that is independent from your AsyncTask and doesn't know about it. For example (copied directly from my own getData() method, inside my Data class, independent from any activities, async tasks, etc.):

while ((line = reader.readLine()) != null) {
    if (Thread.interrupted()) throw new InterruptedException();
    content.append(line);
}

Just make sure to handle the InterruptedException in your doInBackground(), e.g.:

@Override
protected Integer doInBackground(String... params) {
    try {
        return getData();
    }
    catch (InterruptedException e) {
        Log.d("MyApp", "Girl, Interrupted");
        return -1;
    }
}

Also worth noting that onPostExecute() is not called if the task is cancelled. Instead, onCancelled() is called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文