在下载文件时更新通知时如何防止UI滞后?

时间:2013-06-27 14:16:25

标签: java android android-asynctask

我目前正在使用AsyncTask在我的应用中在后台下载大文件,目前下载进度显示为ProgressDialog,通过onProgressUpdate进行更新,如下所示:< / p>

protected String doInBackground(String... sUrl) {
        try {
            String destName = sUrl[1];
            file_Delete(destName); // Just to make sure!

            URL url = new URL(sUrl[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
            int fileLength = connection.getContentLength();

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(destName);

            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();

        } catch (Exception e) {
            Log.e(TAG, NAME + ": Error downloading file! " + e.getMessage());
            return e.getMessage();
        }

        return null;
    }

@Override protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        DownloadImage.mProgressDialog.setProgress(progress[0]);


    }

这很好用,但我现在想在通知栏中使用通知,以便跟踪下载(因为文件可能相当大,用户希望从应用程序外部跟踪)。

我已经尝试了以下代码但是UI开始严重滞后,我可以看到它由于publishProgress被调用很多,所以我怎么能改变后台代码来调用publishProgress只有每一秒

@Override protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        DownloadImage.mProgressDialog.setProgress(progress[0]);

        DownloadImage.myNotification = new NotificationCompat.Builder(c)
        .setContentTitle("Downloading SlapOS")
        .setContentText("Download is " + progress[0] + "% done")
        .setTicker("Downloading...")
        .setOngoing(true)
        .setWhen(System.currentTimeMillis())
        .setProgress(100, progress[0], false)
        .setSmallIcon(R.drawable.icon)
        .build();

        DownloadImage.notificationManager.notify(1, DownloadImage.myNotification);

    }

2 个答案:

答案 0 :(得分:15)

  

所以我怎样才能改变后台代码,每隔一秒调用一次publishProgress

之前我已经完成了上传功能,显示了Notification中的%,但同样的想法。让AsyncTask跟踪percentDone下载的内容,publishProgress更改时调用percentDone。这样,只有在下载%发生变化时才会调用publishProgress,因此Notification需要更新。这应解决UI滞后问题。

我正在写这个作为我建议的实现,听起来OP已经让它工作了。但也许这将有助于未来的其他人:

    byte data[] = new byte[1024];
    long total = 0;
    int count, latestPercentDone;
    int percentDone = -1;
    while ((count = input.read(data)) != -1) {
        total += count;
        latestPercentDone = (int) Math.round(total / fileLength * 100.0);
        if (percentDone != latestPercentDone) {
            percentDone = latestPercentDone;
            publishProgress(percentDone);
        }
        output.write(data, 0, count);
    }

答案 1 :(得分:0)

我真的很喜欢你的做法!我发现将代码更改为以下内容使我的进度条正确更新。我在使用math.round()时遇到了一些问题。

latestPercentDone = (int) ((dataBytesWritten / (float) totalSize) * 100);
    if (percentDone != latestPercentDone) {
    percentDone = latestPercentDone;
    publishProgress(percentDone);
    }