取消文件下载与HttpClient的和AsyncTask的 [英] Cancelling file download with httpclient and asynctask

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

问题描述

在我的应用程序,我需要从URL位置下载文件。我想显示的下载(在通知区域或可选)在DialogBox的进展。我遇到几个很好的资源,在这个问题上(像的http:// progrnotes .blogspot.com / 2010/09 / C-android.html )。不幸的是,所有的例子并不提供有关如何正确地取消每个用户的请求下载一个明确的指示。所以我的问题其实很简单:

In my app I need to download files from url locations. I want to display progress of the download in a dialogbox (or optionally in the notification area). I've come across several good resources on this subject (something like http://progrnotes.blogspot.com/2010/09/c-android.html). Unfortunately, all the examples don't provide a clear indication on how to properly cancel a download per the user's request. So my question is actually quite simple:

给定的AsyncTask,这将下载文件中的背景(用HttpClient的),并与下载进度和取消按钮显示DialogBox的,我怎么能取消下载并停止后台任务按钮时,pressed?

Given an asynctask which downloads the file in the background (with httpclient) and displays a dialogbox with download progress and a cancel button, how do I can cancel the download and stop the background task when the button is pressed?

我知道杀人的线程通常不是一个好主意,所以我可能需要一个cancel'变量在我的后台线程的工作。如何从按钮通信的停止信号AsyncTask的?

I know killing threads is generally not a good idea, so I will probably need to work with a 'cancel'-variable in my background thread. How do I communicate a stop signal from the button to the asynctask?

问候, 伊沃

推荐答案

让你的按钮调用 AsyncTask.cancel(真),然后选中 isCancelled ()从内 doInBackground(参数... PARAMS)。通过这种方式,你可以传达给后台线程的下载应该被取消,并可以采取适当的措施来阻止它。

Have your button call AsyncTask.cancel(true) and then check isCancelled() from inside doInBackground(Params... params). In this manner, you can communicate to the background thread that the download should be cancelled, and you can take the appropriate steps to stop it.

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

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