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

查看:22
本文介绍了如何完全杀死/删除/删除/停止 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.

怎么做?

感谢 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天全站免登陆