DownloadManager.ACTION_DOWNLOAD_COMPLETE 广播接收器多次接收相同的下载 ID,在 Android 中具有不同的下载状态 [英] DownloadManager.ACTION_DOWNLOAD_COMPLETE broadcast receiver receiving same download id more than once with different download statuses in Android
问题描述
我正在使用 Android DownloadManger 系统服务通过以下方式下载一些文件
I am using Android DownloadManger System Service for downloading some files in following way
dwnId = mgr.enqueue(new DownloadManager.Request(serveruri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle(getAlbumName())
.setDescription(getTrackName())
.setDestinationUri(deviceUri)
.setShowRunningNotification(true));
其中 mgr
是下载管理器实例,dwnId
是返回的唯一 ID.我也在注册 ACTION_DOWNLOAD_COMPLETE
where mgr
is Download Manager instance, dwnId
is unique ID returned. I am also registering for ACTION_DOWNLOAD_COMPLETE
registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
在 onDownloadComplete BroadcastReceiver 的 onReceive() 方法中,我得到了下载 ID 像
and in the onDownloadComplete BroadcastReceiver's onReceive() method I am getting download Id like
Long dwnId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
之后我查询下载管理器的下载状态
After that I am querying Download Manager for Download status
Cursor c = downloadManager.query(new DownloadManager.Query().setFilterById(dwnId)); c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
用于 DownloadManager.STATUS_* 常量.
for DownloadManager.STATUS_* constants.
问题是我两次收到相同的 downId(意味着 onReceive 方法被调用两次),一次是 DownloadManager.STATUS_SUCCESSFUL 状态,一次是 DownloadManager.STATUS_FAILED 状态,用于相同的 dwnId.我发出一次下载大约 10 个文件的请求,但在设备下载管理器上,它在通知栏中的左上角显示下载计数为 12 或 13.我认为下载管理器在下载文件和恢复或自动重新启动以再次下载相同的文件时存在一些问题.这就是为什么我请求下载的文件数量与下载队列中的实际数量之间存在差异.正因为如此,我才两次获得相同的 DownloadId 完成操作.如果这是真的,如何限制它.我错了,我请求的实际下载之间计数差异的原因可能是什么?为什么广播接收器两次收到相同的下载 ID.有没有人可以告诉我一下?
The problem is I am receiving the same downId twice (means onReceive method is called twice), once with DownloadManager.STATUS_SUCCESSFUL status and once with DownloadManager.STATUS_FAILED status for same dwnId. I am issuing request to download some 10 files at a time and but on device download manager it is showing the download count as some 12 or 13 in the notification bar top left means. I think that Download manager has some problem in downloading files and resumed or automatically restarted to download the same file again. Thats why there is a difference between the files count I requested to download and actual number in download queue. Because of this only I am getting same DownloadId complete action twice. If this is true, how to restrict it. Am I wrong what might be the reason for count difference between what I requested to actual download? Why is the broadcast receiver receiving the same download Id twice. Can anybody please let me know?
提前致谢...
推荐答案
这是一个已报告的错误,请参阅:http://code.google.com/p/android/issues/detail?id=18462
This is a reported bug see: http://code.google.com/p/android/issues/detail?id=18462
我发现的解决方法是验证下载是否成功,如果没有放弃意图或如果从未下载过文件则重新排队...
The way around I found is to verify if the download was a success, if not ditch the intent or re-queue the file if it was never downloaded...
花了几个小时才弄明白:(
Lost a couple of hours figuring that one :(
** 添加代码示例 **
** adding code example **
/**
* Check if download was valid, see issue
* http://code.google.com/p/android/issues/detail?id=18462
* @param long1
* @return
*/
private boolean validDownload(long downloadId) {
Log.d(TAG,"Checking download status for id: " + downloadId);
//Verify if download is a success
Cursor c= dMgr.query(new DownloadManager.Query().setFilterById(downloadId));
if(c.moveToFirst()){
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if(status == DownloadManager.STATUS_SUCCESSFUL){
return true; //Download is valid, celebrate
}else{
int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON));
Log.d(TAG, "Download not correct, status [" + status + "] reason [" + reason + "]");
return false;
}
}
return false;
}
这篇关于DownloadManager.ACTION_DOWNLOAD_COMPLETE 广播接收器多次接收相同的下载 ID,在 Android 中具有不同的下载状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!