问题
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));
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));
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));
for DownloadManager.STATUS_* constants.
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?
Thanks In Advance...
回答1:
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 :(
** Edit: 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;
}
For complete code see : https://github.com/flegare/JAV387_LaboWidget/blob/master/src/com/mobidroid/widgetfact/service/FactService.java
回答2:
A simple way to download your files, See download progress in notification bar and even open your file when it complete to download by just clicking it in the notification bar.
Just call this method and pass your filename and download url
public void downloadFile(String name, String url){
//download link
downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
//allow download to take place over wifi, mobile network and roaming
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE ).setAllowedOverRoaming(true);
request.setAllowedOverRoaming(false);
//name to show while downloading
request.setTitle(name);
//description to show while downloading
request.setDescription("Downloading " + name);
//show on navigation
request.setVisibleInDownloadsUi(true);
//download path
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS.toString(), "/" + name);
//file open when item on navigation is clicked
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
long downloadId = downloadManager.enqueue(request);
}
Output
You can learn more here
来源:https://stackoverflow.com/questions/8937817/downloadmanager-action-download-complete-broadcast-receiver-receiving-same-downl