取消所有 AsyncTask? [英] Cancel all AsyncTask?

查看:19
本文介绍了取消所有 AsyncTask?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于获取媒体文件缩略图的类.这个类似 Loader 的类为每个 ImageView 启动一个 AsyncTask(在 SomeAdapter#getView() 中调用).任务本身做了很多事情,其中​​之一是调用 DiskLruCache,但是当 SD 卡被卸载,而任务仍在运行时,应用程序崩溃了.

I have a class which is used to get media file thumbs. This Loader like class starts an AsyncTask for every ImageView (is called in SomeAdapter#getView()). The task itself does a lot of things, and one of them is calling DiskLruCache, but when the SD card is unmounted, while the tasks are still running the application crashes.

如果卡状态发生变化,我知道如何注册.

I know how to register if the card state is changed.

所以我需要一种方法来停止所有正在运行的任务.你能帮忙的话,我会很高兴.

So I need an approach how to stop all the running tasks. Any help would be nice.

推荐答案

只需遍历所有列表 &使用以下将取消所有正在运行的异步任务.

just iterate through all list & use the following which will cancel all running asynctask.

if(myAsyncTask.getStatus().equals(AsyncTask.Status.RUNNING))
 {
     myAsyncTask.cancel(true);
 }

这篇关于取消所有 AsyncTask?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆