Search code examples
androidandroid-download-manager

Android Download Manager. status is always pending


I try to use Download Manager to download some files form specific URL, but the download request was never completed.

So I log some information to see what went wrong, it turns out the request is always in pending status, and the COLUMN_REASON is 0 which I couldn't find the corresponding description on the document.

COLUMN_STATUS: 1
COLUMN_REASON: 0
COLUMN_TOTAL_SIZE_BYTES: -1
COLUMN_BYTES_DOWNLOADED_SO_FAR: 0

Here is how to start a download.

val req = DownloadManager.Request(uri).apply {
    addRequestHeader("Cookie", cookie)
    allowScanningByMediaScanner()
    setTitle(fullname)
    setDescription(/* description text */)
    setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, fullname)
}
val downloadId = downloadManager.enqueue(req)

And log information for debugging.

        val filterQuery = DownloadManager.Query().setFilterById(downloadId)
        val cursor = downloadManager.query(filterQuery)
        if (cursor.moveToFirst()) {
            val total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))
            val current = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))
            val status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
            val reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON))

            Log.d("App", "status: " + status.toString())
            Log.d("App", "reason: " + reason.toString())
            Log.d("App", "total: " + total.toString())
            Log.d("App", "current: " +  current.toString())
        }

So what's a possible reason that status of request was always pending and how do I debug it?

Any help is going to be appreciated.


Solution

  • In my case, settings up a VPN seem to solve this problem. It looks like google services have been blocked in my network and after I set up a system global VPN the issue has gone.