停止在Android中的的AsyncTask

停止在Android中的的AsyncTask

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

问题描述

我做了一个应用程序,下载影片从我们的服务器。问题是:

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

当我取消下载我呼吁:

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(真)不取消我的任务是什么?我不希望它再在背景上。我只是想彻底关闭它,如果我点击取消。

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.

例如你正在下载中,而在视频doInBackground(..) / for循环。

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;
     }

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

09-01 18:23