使用 DownloadManager 在进度条中显示下载进度

showing download progress in progress bar using DownloadManager

我正在开发一个 android 应用程序,我在其中使用 DownloadManager 从服务器下载文件。

问题

在文件下载过程中,我想通过progress bar显示下载进度。文件下载成功,但无法显示下载进度

我用来显示进度的代码

private void startAppDownload() {

    ...

    // code to show download progress
    new Thread(new Runnable() {
        @Override
        public void run() {
            boolean isDownloading = true;
            int downloadStatus, totalBytesDownloaded, totalBytes;

            DownloadManager.Query downloadQuery = new DownloadManager.Query();
            downloadQuery.setFilterById(downloadID);

            Cursor cursor = downloadManager.query(downloadQuery);
            cursor.moveToFirst();

            while (isDownloading) {

                totalBytesDownloaded = cursor.getInt(
                        cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
                );

                totalBytes = cursor.getInt(
                        cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
                );

                downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));

                if(downloadStatus == DownloadManager.STATUS_SUCCESSFUL) {
                    isDownloading = false;
                }

                final int downloadProgress = (int) ((double)totalBytesDownloaded / (double)totalBytes * 100f);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        downloadProgressBar.setProgress(downloadProgress);
                    }
                });
            }

            cursor.close();
        }
    }).start();
}

我记录了 totalBytesDownloaded 变量,但它总是 zero 并且 totalBytes 变量总是 -1.

这导致 downloadProgress 变量始终为 zero,因此 progress bar 没有显示任何进度。

问题

我在这里做错了什么?为什么 totalBytesDownloaded 变量总是等于 zerototalBytes 变量总是 -1

您有一个逻辑错误,您的查询超出了更新 UI 的 while 循环

private void startAppDownload() {


    new Thread(new Runnable() {
        @Override
        public void run() {
            boolean isDownloading = true;
            int downloadStatus, totalBytesDownloaded, totalBytes;



            while (isDownloading) {

                DownloadManager.Query downloadQuery = new DownloadManager.Query();
                downloadQuery.setFilterById(downloadID);

                Cursor cursor = downloadManager.query(downloadQuery);
                cursor.moveToFirst();

                totalBytesDownloaded = cursor.getInt(
                        cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
                );

                totalBytes = cursor.getInt(
                        cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
                );

                downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));

                if(downloadStatus == DownloadManager.STATUS_SUCCESSFUL) {
                    isDownloading = false;
                    break;;
                }

                final int downloadProgress = (int) ((double)totalBytesDownloaded / (double)totalBytes * 100f);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        downloadProgressBar.setProgress(downloadProgress);
                    }
                });
            }

            cursor.close();
        }
    }).start();
}