DownloadManager 下载进度不可见

DownloadManager download progress not visible

我正在尝试使用 Android 的 DownloadManager 下载文件,并尝试使用日志语句在控制台打印下载。 虽然文件已正确下载,但我看不到下载进度的日志语句 这是我的代码

 private fun downloadPdf(fileName: String?, fileExtension: String?, destinationDirectory: String?, url: String?) {
        val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
        val uri = Uri.parse(url)
        val request = DownloadManager.Request(uri)
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
        request.setDestinationInExternalPublicDir(destinationDirectory, fileName + fileExtension)
        val downloadId = downloadManager.enqueue(request)

        thread {
            val query = DownloadManager.Query()
            query.setFilterById(downloadId)

            val cursor = downloadManager.query(query)

            if(cursor.moveToFirst()){
                val sizeIndex = cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
                val downloadedIndex = cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
                val size = cursor.getInt(sizeIndex)
                val downloaded = cursor.getInt(downloadedIndex)

                val progress: Long
                if(size != -1){
                    progress = downloaded * 100L / size
                    runOnUiThread {
                        Log.i("pritishsawantprogress",progress.toString())
                    }
                }
            }
        }
    }

如有任何帮助,我们将不胜感激

  private fun downloadPdf(fileName: String?, fileExtension: String?, destinationDirectory: String?, url: String?) {
        val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
        val uri = Uri.parse(url)
        val request = DownloadManager.Request(uri)
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
        request.setDestinationInExternalPublicDir(destinationDirectory, fileName + fileExtension)
        val downloadId = downloadManager.enqueue(request)

        thread {
            var downloading = true
            while (downloading){
                val query = DownloadManager.Query()
                query.setFilterById(downloadId)

                val cursor = downloadManager.query(query)
                if(cursor.moveToFirst()){
                    val bytesDownloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))
                    val bytesTotal = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))

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

                    val progress = ((bytesDownloaded * 100L)/bytesTotal).toInt()
                    runOnUiThread {
                        Log.i("pritishsawantprogress",progress.toString())
                    }

                    cursor.close()
                }
            }
        }

    }