如何完全杀死/删除/删除/停止AsyncTask [英] How to completely kill/remove/delete/stop an AsyncTask

查看:106
本文介绍了如何完全杀死/删除/删除/停止AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个可从我们的服务器下载视频的应用. 问题是:

I made an app that downloads videos from our server. The issue is:

当我取消下载时,我会致电:

When i cancel the downloading i call:

myAsyncTask.cancel(true)

我注意到,myAsyncTask不会在调用取消时停止...我的ProgressDialog仍然会上升,就像从状态跳转到状态一样,向我显示每次我取消并重新启动AsyncTask单击下载按钮,新的AsyncTask开始... 每次单击下载..然后取消,然后再次下载一个单独的AsyncTask开始.

I noticed, that myAsyncTask doesn't stops on calling cancel... my ProgressDialog still goes up and its like jumping from status to status showing me that each time I cancel and start again an AsyncTask by clicking the download button, a new AsyncTask starts... Each time I click download.. then cancel, then again download a separate AsyncTask starts.

为什么myAsynTask.cancle(true)不取消我的任务?我不想在背景上使用它了.我只想单击取消"就完全关闭它.

Why is myAsynTask.cancle(true) not cancelling my task ? I don't want it anymore on the background. I just want to completely shut it down if I click cancel.

该怎么做?

E D I T:

感谢gtumca-MAC,以及其他通过以下方式帮助我的人:

Thanks to gtumca-MAC, and the others who helped me did it by:

while (((count = input.read(data)) != -1) && (this.isCancelled()==false)) 
{
    total += count;
    publishProgress((int) (total * 100 / lenghtOfFile));
    output.write(data, 0, count);
}

谢谢!

推荐答案

AsyncTask不会取消进程

AsyncTask does not cancel process on

myAsynTask.cancel(true) 

为此,您必须手动将其停止.

For that you have to stop it manually.

例如,您正在while/for循环中的doInBackground(..)中下载视频.

for example you are downloading video in doInBackground(..) in while/for loop.

protected Long doInBackground(URL... urls) {

         for (int i = 0; i < count; i++) {
          // you need to break your loop on particular condition here

             if(isCancelled())
                  break;             
         }
         return totalSize;
     }

这篇关于如何完全杀死/删除/删除/停止AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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