Android Download Manager Download Same File Twice

Viewed 275

I am using Android Download Manager to download a large zip file. The Download Manager is working fine. But it is downloading the same file twice in one single request giving two different download IDs.

I am not able to find why it is doing so. I just want the file should be downloaded only once. And if it is failed, the User can go for manual redownloading, no automatic redownload on fail as well.

Below is my code:

DownloadManager.Request mediaDownloadRequests = new DownloadManager.Request(Uri.parse(sDownloadableFileURL))
                        .setTitle(mContext.getResources().getString(R.string.app_name))// Title of the Download Notification
                        .setDescription(sDownloadingMsg)// Description of the Download Notification
                        .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)// Visibility of the download Notification
                        .setDestinationUri(Uri.fromFile(zip_mmpk_file)) // Uri of the destination file
                        .setAllowedOverMetered(true)// Set if download is allowed on Mobile network
                        .setAllowedOverRoaming(true)// Set if download is allowed on roaming network
                        .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                downloadManager= (DownloadManager) mContext.getSystemService(mContext.DOWNLOAD_SERVICE);
                long downloadID = downloadManager.enqueue(mediaDownloadRequests));
    Log.e("Downloading For --> ", "URL : "+sDownloadableFileURL + " || Download ID : "downloadID);

And my Broadcast Receiver is as below:

private BroadcastReceiver onMediasDownloadComplete = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //Fetching the download id received with the broadcast
            long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            DownloadManager.Query q = new DownloadManager.Query();
            q.setFilterById(id);
            Cursor c = downloadManager.query(q);
            if (c.moveToFirst()) {
                if (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                    String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                    performOnDownloadSuccessOperation(id, uriString);
                } else {
                    performOnDownloadFailedOperation(id);
                }
            }
        }
    };

I am registering my download Broadcast Receiver in the

onCreateView()

method of the Fragment as given below:

mContext.registerReceiver(onMediasDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

and Unregistering the Receiver in the

onDestroyView()

method of the same fragment as :

mContext.unregisterReceiver(onMediasDownloadComplete);

Can anyone help me why the Download Manager is downloading the same file twice?

0 Answers
Related