在活动中使用DownloadManager显示下载进度

94

我正在尝试在我的应用程序中重现DownloadManager在通知栏中显示的进度,但是我的进度从未被发布。我试图使用runOnUiThread()来更新它,但由于某些原因它没有被更新。

我的下载:

String urlDownload = "https://dl.dropbox.com/s/ex4clsfmiu142dy/test.zip?token_hash=AAGD-XcBL8C3flflkmxjbzdr7_2W_i6CZ_3rM5zQpUCYaw&dl=1";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlDownload));

request.setDescription("Testando");
request.setTitle("Download");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "teste.zip");

final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

final long downloadId = manager.enqueue(request);

final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);

new Thread(new Runnable() {

    @Override
    public void run() {

        boolean downloading = true;

        while (downloading) {

            DownloadManager.Query q = new DownloadManager.Query();
            q.setFilterById(downloadId);

            Cursor cursor = manager.query(q);
            cursor.moveToFirst();
            int bytes_downloaded = cursor.getInt(cursor
                    .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
            int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

            if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                downloading = false;
            }

            final double dl_progress = (bytes_downloaded / bytes_total) * 100;

            runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    mProgressBar.setProgress((int) dl_progress);

                }
            });

            Log.d(Constants.MAIN_VIEW_ACTIVITY, statusMessage(cursor));
            cursor.close();
        }

    }
}).start();

我的statusMessage方法:

private String statusMessage(Cursor c) {
    String msg = "???";

    switch (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
    case DownloadManager.STATUS_FAILED:
        msg = "Download failed!";
        break;

    case DownloadManager.STATUS_PAUSED:
        msg = "Download paused!";
        break;

    case DownloadManager.STATUS_PENDING:
        msg = "Download pending!";
        break;

    case DownloadManager.STATUS_RUNNING:
        msg = "Download in progress!";
        break;

    case DownloadManager.STATUS_SUCCESSFUL:
        msg = "Download complete!";
        break;

    default:
        msg = "Download is nowhere in sight";
        break;
    }

    return (msg);
}

我的日志记录正常工作,但当我的下载正在运行时,显示“正在下载!”,当下载完成时显示“下载完成!” ,但是同样的情况没有发生在我的进度条上,为什么?我真的需要一些帮助,非常感谢其他的解决方法。


你的文件可能太小了,下载完成之前进度就已经发布了,这是可能的吗?你的任务返回的下载查询结果是什么?如果任务在一段时间后才执行,那么你的主线程上可能有其他长时间运行的操作。 - Paul Lammertsma
我更新了代码,现在你能看一下吗?至于文件长度,它不是太小,我可以在通知栏上看到下载进度。 - Victor Laerte
已经回答了这个问题https://dev59.com/u1kJtIcB2Jgan1znExEn#73546957。 - Quratulain Naqvi
5个回答

68

你正在将两个整数相除:

final double dl_progress = (bytes_downloaded / bytes_total) * 100;

由于 bytes_downloaded 小于 bytes_total,所以 (bytes_downloaded / bytes_total) 的结果将为0,因此您的进度将始终为0。

请更改您的计算方法为

final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);

获得整体(尽管向下取整)百分位数的进度。


@AZ_ 感谢您的贡献。我建议您添加自己的答案,提供更详细的解决方案。 - Paul Lammertsma
没关系,我不想再提供一个已经被接受的答案,因为这会给用户带来困难。您可以选择不接受我的编辑 :) - AZ_
1
如果您的活动结束并希望取消下载,则会出现除以零致命错误。这就是为什么我这样做的原因: final int dl_progress = ( bytes_total > 0 ? (int) ((bytes_downloaded * 100L) / bytes_total) : 0 ); - KaHa6uc

19

保罗的回答是正确的,但是如果下载量很大,您很快就会达到最大整数并开始出现负进度。我使用以下方法解决了这个问题:

final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);

你是对的;我已经修改了我的答案,以确保其他人不会犯同样的错误。 - Paul Lammertsma

8

如果有人需要在Kotlin中使用RxJava实现@Victor Laerte的下载进度检索器,这里是代码:

DownloadStateRetriever.kt

class DownloadStateRetriever(private val downloadManager: DownloadManager) {

    fun retrieve(id: Long) {
        var downloading = AtomicBoolean(true)

        val disposable = Observable.fromCallable {
            val query = DownloadManager.Query().setFilterById(id)
            val cursor = downloadManager.query(query)

            cursor.moveToFirst()

            val bytesDownloaded = cursor.intValue(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
            val bytesTotal = cursor.intValue(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)

            if (isSuccessful(cursor)) downloading.set(false)
            cursor.close()

            if (bytesTotal == 0) 0.toInt() else ((bytesDownloaded * 100F) / bytesTotal).toInt()
        }
                .subscribeOn(Schedulers.newThread())
                .delay(1, TimeUnit.SECONDS)
                .repeatUntil { !downloading.get() }
                .subscribe {
                    Timber.i("Subscribed to $id. progress: $it")
                }
    }

    private fun isSuccessful(cursor: Cursor) = status(cursor) == DownloadManager.STATUS_SUCCESSFUL

    private fun status(cursor: Cursor) = cursor.intValue(DownloadManager.COLUMN_STATUS)
}

我已经为光标添加了扩展,以便更清晰地显示代码:

CursorExtensions.kt

import android.database.Cursor

fun Cursor.column(which: String) = this.getColumnIndex(which)
fun Cursor.intValue(which: String): Int = this.getInt(column(which))
fun Cursor.floatValue(which: String): Float = this.getFloat(column(which))
fun Cursor.stringValue(which: String): String = this.getString(column(which))
fun Cursor.doubleValue(which: String): Double = this.getDouble(column(which))


4

正如Paul所说,你正在使用整数进行除法运算,结果始终<1。

在除法运算之前始终将数字转换为浮点型以进行计算并返回结果。

不要忘记处理DivByZero异常。

final int dl_progress = (int) ((double)bytes_downloaded / (double)bytes_total * 100f);

2

如果有人需要使用Kotlin和Flows实现下载进度,我希望这篇文章能对他们有所帮助。这是@rahimli版本的内容,使用了Flows而不是RxJava。

Retriever

private fun retrieve(id: Long) = flow {
        val downloading = AtomicBoolean(true)

        while (downloading.get()) {
            val query = DownloadManager.Query().setFilterById(id)
            val cursor = downloadManager.query(query)

            cursor.moveToFirst()

            val bytesDownloaded = cursor.intValue(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
            val bytesTotal = cursor.intValue(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)

            if (isSuccessful(cursor)) downloading.set(false)
           
            cursor.close()

            emit(DownloadingState.Downloading(bytesDownloaded, bytesTotal))

            if (downloading.get()) delay(1000)
        }
    }.flowOn(Dispatchers.IO)

一些辅助工具/扩展

    private fun isSuccessful(cursor: Cursor) = status(cursor) == DownloadManager.STATUS_SUCCESSFUL
    private fun status(cursor: Cursor) = cursor.intValue(DownloadManager.COLUMN_STATUS)
    private fun Cursor.column(which: String) = this.getColumnIndex(which)
    private fun Cursor.intValue(which: String): Int = this.getInt(column(which))

    sealed class DownloadingState {
        data class Downloading(val downloadedBytes: Int, val totalBytes: Int) : DownloadingState() {
            val progress = if (totalBytes == 0) 0 else ((downloadedBytes * 100) / totalBytes)
        }
        object Failure : DownloadingState()
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接