Android的下载站 [英] Android stop download

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

问题描述

在我的应用程序下载和解析HTML页面。不过,我希望能够停止在其轨道上下载(即当用户点击取消)。

这是code我现在用的,这是从doInBackground称为从AsyncTask的。

如何取消从AsyncTask的境外申请?

我目前使用 htmlcleaner

  HtmlCleaner清洁=新HtmlCleaner();
   CleanerProperties道具= cleaner.getProperties();
   props.setAllowHtmlInsideAttributes(真);
   props.setAllowMultiWordAttributes(真);
   props.setRecognizeUni codeChars(真);
   props.setOmitComments(真);
   尝试{
        网址URL =新的URL(urlstring);
        康涅狄格州的URLConnection = url.openConnection();
        TagNode节点= cleaner.clean(新的InputStreamReader(conn.getInputStream()));
        返回节点;
   }赶上(例外五){
       失败= TRUE;
       返回;
   }


解决方案

好吧,我相信我已经解决了这一点。

在我的Activity类我有一个变量(布尔)失败。另外,我有延伸的AsyncTask 在活动范围内的私人下载类。这样,下载类可以访问失败布尔值。当活动启动时,它会启动下载任务和进度对话框弹出。当任务完成后,关闭对话框,然后继续处理下载的内容。

然而,当用户取消进度对话框,失败设置为true,用户通过调用送回previous活动完成。在此期间,下载还在忙着下载。因为现在的结果不必要的,我们希望它停止使用的资源尽快。为了做到这一点,我在尽可能多的步骤,尽可能打破了 doInBackground 方法。每个步骤后我检查失败当它被设置为真还是,它根本不进入下一个步骤。看到它在下面的动作。 Furthemore,在的BufferedReader读是公开的,在 onCancelled 方法我执行 reader.close ()。这将抛出各种异常,但这些都是正确捕获。

 公共无效DoInBackground(.........){
    尝试{
        网址URL =新的URL(URI);
        康涅狄格州的URLConnection = url.openConnection();
        如果(!失败){
            ISR =新的InputStreamReader(conn.getInputStream());
            如果(!失败){
                读者=新的BufferedReader(ISR);
                publishProgress(1);
                如果(!失败){
                    TagNode节点= cleaner.clean(读卡器);
                    publishProgress(2);
                    返回节点;
                }
            }
        }
     }赶上(例外五){
          失败= TRUE;
          Log.v(错误,+ E);
     }
 }@覆盖
保护无效onCancelled(){
    失败= TRUE;
    如果(读者!= NULL)
        尝试{
            reader.close();
        }赶上(IOException异常五){
            失败= TRUE;
        }
    如果(ISR!= NULL)
        尝试{
            isr.close();
        }赶上(IOException异常五){
        }
}

我知道我可以,即使更细小位已经分手了下载过程,但我下载非常小的文件,所以它不是那么重要。

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

解决方案

Ok, I believe I've solved this.

In my Activity class I have a variable (boolean) failed. Also, I have a private Downloader class within the activity which extends ASyncTask. This way, the Downloader class has access to the failed boolean. When the Activity launches, it starts the Downloader 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 to finished. 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 the doInBackground method in as much steps as possible. After each step I check if failed is still false, when it is set to true, it simply doesn't go to the next step. See it in action below. Furthemore, the BufferedReader reader is public, and in the onCancelled method I execute reader.close(). This will throw all sorts of exceptions, but these are properly caught.

 public void DoInBackground(.........) {
    try {
        URL url = new URL(uri);
        URLConnection conn = url.openConnection();
        if (!failed) {
            isr = new InputStreamReader(conn.getInputStream());
            if (!failed) {
                reader = new BufferedReader(isr);
                publishProgress(1);
                if (!failed) {
                    TagNode node = cleaner.clean(reader);
                    publishProgress(2);
                    return node;
                }
            }
        }
     } catch (Exception e) {
          failed = true;
          Log.v("error",""+e);
     } 
 }

@Override
protected void onCancelled() {
    failed = true;
    if (reader != null)
        try {
            reader.close();
        } catch (IOException e) {
            failed = true;
        }
    if (isr != null)
        try {
            isr.close();
        } catch (IOException e) {
        }
}

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.

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

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