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

Zac*_*ell 8 java android android-asynctask

我目前正在使用AsyncTask我的应用程序中的后台下载大文件,目前下载进度显示为ProgressDialog更新onProgressUpdate,如下所示:

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]);


    }
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我现在想在通知栏中使用通知,因此请跟踪下载(因为文件可能相当大,用户希望从应用程序外部跟踪).

我已经尝试了下面的代码,但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);

    }
Run Code Online (Sandbox Code Playgroud)

Ste*_*yle 17

那么我怎样才能改变后台代码,每秒只调用一次publishProgress

我之前已经完成了上传功能,它显示了%Notification,但同样精确的想法.让您AsyncTask跟踪percentDone下载内容,并且publishProgresspercentDone更改时调用.这样,您只会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);
    }
Run Code Online (Sandbox Code Playgroud)