DownloadManager.ACTION_DOWNLOAD_COMPLETE broadcast receiver receiving same download id more than once with different download statuses in Android

泄露秘密 提交于 2019-11-27 11:28:56

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!