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 ou
Solution 1:
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.
Solution 2:
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] StartingW/DownloadManager: [1988] Stop requested with status 500:InternalServerErrorD/DownloadManager: [1988] FinishedwithstatusWAITING_TO_RETRY
and
W/DownloadManager: [1988] Stop requested with status 403: Unhandled HTTP response:403Forbidden
Solution 3:
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:
privatevoidstartDownload(View v)
{
Uri uri=Uri.parse("http://example.com/app/name.apk");
DownloadManagermgr= (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Requestreq=newDownloadManager.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
privatevoidgetDownloadStatus()
{
DownloadManager.Queryquery=newDownloadManager.Query();
query.setFilterById(downloadId);
Cursorcursor= ((DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE))
.query(query);
if (cursor.moveToFirst())
{
finalTimertimer=newTimer();
timer.schedule(newTimerTask() {
@Overridepublicvoidrun() {
query.setFilterById(downloadId);
Cursorcursor= ((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");
}elseif (status == DownloadManager.STATUS_SUCCESSFUL) {
Log.i("DM_STATUS","status is "+" success");
timer.cancel();
}
}
}, 100,1);
}
}
Post a Comment for "Android Download Manager. Status Is Always Pending"