安卓停止下载
在我的应用程序中,我下载并解析一个 html 页面。但是,我希望能够停止下载(即当用户点击取消时)。
这是我现在使用的代码,它是从 ASyncTask 的 doInBackground 调用的。
如何从 ASyncTask 外部取消此请求?
我目前使用 htmlcleaner
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties props = cleaner.getProperties();
props.setAllowHtmlInsideAttributes(true);
props.setAllowMultiWordAttributes(true);
props.setRecognizeUnicodeChars(true);
props.setOmitComments(true);
try {
URL url = new URL(urlstring);
URLConnection conn = url.openConnection();
TagNode node = cleaner.clean(new InputStreamReader(conn.getInputStream()));
return node;
} catch (Exception e) {
failed = true;
return;
}
In my application I download and parse a html page. However, I want to be able to stop the download in its tracks (i.e. when the user hits cancel).
This is the code I use now, which is being called from doInBackground from ASyncTask.
How do I cancel this request from outside of the ASyncTask?
I currently use htmlcleaner
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties props = cleaner.getProperties();
props.setAllowHtmlInsideAttributes(true);
props.setAllowMultiWordAttributes(true);
props.setRecognizeUnicodeChars(true);
props.setOmitComments(true);
try {
URL url = new URL(urlstring);
URLConnection conn = url.openConnection();
TagNode node = cleaner.clean(new InputStreamReader(conn.getInputStream()));
return node;
} catch (Exception e) {
failed = true;
return;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能使用
AsyncTask.cancel()
吗?然后您应该能够使用onCancelled
回调返回到主活动。Can't you use
AsyncTask.cancel()
? You should be able to then use theonCancelled
callback to return to the main activity..好吧,我相信我已经解决了这个问题。
在我的 Activity 类中,我有一个变量(布尔值)
failed
。另外,我在活动中有一个私有的Downloader
类,它扩展了ASyncTask
。这样,Downloader
类就可以访问failed
布尔值。当 Activity 启动时,它会启动Downloader
任务并弹出一个进度对话框。任务完成后,它会关闭对话框,然后继续处理下载的内容。但是,当用户取消进度对话框时,
failed
设置为 true,并且通过调用finished
将用户发送回上一个活动。与此同时,Downloader
仍在忙于下载。因为现在结果是不必要的,我们希望它尽快停止使用资源。为了实现这一目标,我将doInBackground
方法分解为尽可能多的步骤。在每个步骤之后,我都会检查failed
是否仍为false
,当它设置为true
时,它根本不会进入下一步。请参阅下面的实际操作。此外,BufferedReader reader 是公共的,并且在 onCancelled 方法中我执行 reader.close() 。这会抛出各种异常,但这些异常都会被正确捕获。我知道我可以将下载过程分解为更小的部分,但我下载的文件非常小,所以这并不重要。
Ok, I believe I've solved this.
In my Activity class I have a variable (boolean)
failed
. Also, I have a privateDownloader
class within the activity which extendsASyncTask
. This way, theDownloader
class has access to thefailed
boolean. When the Activity launches, it starts theDownloader
task and a progress dialog pops up. When the task finishes, it closes the dialog and then goes on processing the downloaded content.However, when the user cancels the progress dialog,
failed
is set to true, and the user is sent back to the previous activity by a call tofinished
. In the meantime,Downloader
is still busy downloading. Because the results are now unneccessary, we want it to stop using resources asap. In order to accomplish this, I have broken up thedoInBackground
method in as much steps as possible. After each step I check iffailed
is stillfalse
, when it is set totrue
, it simply doesn't go to the next step. See it in action below. Furthemore, theBufferedReader reader
is public, and in theonCancelled
method I executereader.close()
. This will throw all sorts of exceptions, but these are properly caught.I know that I could have broken up the downloading process in even tinier bits, but I am downloading very small files, so it's not that important.