Reputation: 3258
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.
Upvotes: 4
Views: 3554
Reputation: 149
You have to wait(delay) before checking the status or set the download ID every time in a timer. enqueue seems to return the download ID too late. My code works very well:
private void startDownload(View v)
{
Uri uri=Uri.parse("http://example.com/app/name.apk");
DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request req = new DownloadManager.Request(uri)
.setTitle(title)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
|DownloadManager.Request.NETWORK_MOBILE)
.setDescription("downloading")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"name.apk");
downloadId = mgr.enqueue(req);
getDownloadStatus();
}
check status method
private void getDownloadStatus()
{
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor cursor = ((DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE))
.query(query);
if (cursor.moveToFirst())
{
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
query.setFilterById(downloadId);
Cursor cursor = ((DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE))
.query(query);
cursor.moveToFirst();
int status=cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_RUNNING) {
Log.i("DM_STATUS","status is "+" running");
}else if (status == DownloadManager.STATUS_SUCCESSFUL) {
Log.i("DM_STATUS","status is "+" success");
timer.cancel();
}
}
}, 100,1);
}
}
Upvotes: 0
Reputation: 7794
DownloadManager outputs its logs to logcat but not under your application's id, so you'll need to show logs for all apps. There should clues to the failed download in there. For example, here are a couple of my failure cases.
D/DownloadManager: [1988] Starting
W/DownloadManager: [1988] Stop requested with status 500: Internal Server Error
D/DownloadManager: [1988] Finished with status WAITING_TO_RETRY
and
W/DownloadManager: [1988] Stop requested with status 403: Unhandled HTTP response: 403 Forbidden
Upvotes: 1
Reputation: 3258
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.
Upvotes: 1