繁体   English   中英

取消后,AsyncTask正在发布进度

[英]AsyncTask is publishing progress after cancelled

我的情况是可以下载发票的RecyclerView。 他们具有启动下载并在“下载”按钮本身周围的圆形进度视图中显示进度的操作。 我使用AsyncTask通过自定义侦听器下载PDF格式的特定发票,因此我知道何时在AsyncTask本身之外取消,执行和更新进度(以避免在AsyncTask内部通过View并对其进行更新)。

到目前为止,这是我的AsyncTask:

public class DownloadTask extends AsyncTask<URL, Integer, String> implements Codes {

    private AsyncTaskProgressListener listener;
    private File file;


    public DownloadTask(AsyncTaskProgressListener listener) {
        this.listener = listener;
    }

    @Override
    protected String doInBackground(URL... urls) {
        InputStream input = null;
        OutputStream output = null;
        HttpURLConnection connection = null;
        try {
            URL url = urls[0];
            file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + url.getFile());
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return "Server returned HTTP " + connection.getResponseCode()
                        + " " + connection.getResponseMessage();
            }
            int fileLength = connection.getContentLength();
            LogUtil.log("" + fileLength);
            // download the file
            input = connection.getInputStream();
            output = new FileOutputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), url.getFile()));

            byte data[] = new byte[4096];
            long total = 0;
            int count;
            int lastProgress = 0;
            while ((count = input.read(data)) != -1) {
                if (isCancelled()) {
                    break;
                }
                total += count;
                // publishing the progress....
                int progress = 0;
                if (fileLength > 0) {
                    // only if total length is known
                    progress = (int) (total * 100 / fileLength);
                }
                //Prevent publishing the same progress various times as it would freeze the progress bar.
                if (progress > lastProgress) {
                    publishProgress(progress);
                    lastProgress = progress;
                }
                output.write(data, 0, count);
            }

        } catch (Exception e) {
            LogUtil.log(e.toString());
            return e.toString();
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (connection != null)
                connection.disconnect();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        listener.onProgressUpdated(values[0]);
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        listener.onCompleted();
    }

    @Override
    protected void onCancelled(String s) {
        listener.onCancel();
        file.delete();
        LogUtil.log("CANCELED");
        super.onCancelled(s);
        //noinspection ResultOfMethodCallIgnored
    }

}

而我的听众:

tasks[0] = new DownloadTask(new AsyncTaskProgressListener() {
                                        @Override
                                        public void onCompleted() {
                                            state = INVOICE_DOWNLOADED;
                                        }

                                        @Override
                                        public void onCancel() {
                                            state = INVOICE_NOT_DOWNLOADED;
                                            progressView.resetAnimation();
                                        }

                                        @Override
                                        public void onProgressUpdated(int progress) {
                                            progressView.setProgress(progress);
                                        }

                                    });

当我按下取消按钮时,这就是所谓的:

progressView.setProgress(0);
if (tasks[0] != null) 
    tasks[0].cancel(true);

最后是我的问题:我发现当取消下载时(主要是在下载50%之后onProgressUpdated(progress)progressView.setProgress(0)之后最后一次调用了onProgressUpdated(progress) ,因此取消了下载,但进度条卡住了最后的进度而不是0(设置新进度时,它会以非常酷的方式进行动画处理)。

我试图以各种方式来解决此,就像上的AsyncTask一个布尔值,知道它是否取消,它设置为true的方法,以及它在一个检查onProgressUpdated(progress)的的AsyncTask的调用之前onProgressUpdated(progress)设置视图进度的侦听器; 我无法找到解决方案。

onProgressUpdated()progressView.setProgress(0); 两者都在UI线程上运行,因此您可以对其添加布尔检查。

  1. 添加一个布尔字段boolean noMoreProgressUpdate = false
  2. progressView.setProgress(0);之后添加noMoreProgressUpdate = true progressView.setProgress(0);
  3. 更改

    public void onProgressUpdated(int progress) { progressView.setProgress(progress); }

 `
    public void onProgressUpdated(int progress) {
        if(!noMoreProgressUpdate) {
            progressView.setProgress(progress);
        }
    }
 `

我会用

onPreExecute() {
  //do progress bar here
}

然后在onPostExecute()

onPostExecute(String result) {
   //dismiss progress bar before anything else
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM