Android: Download Manager resume issue - android

I am trying to download file using Android Download Manager. As per docs:
The download manager will conduct the download in the background, taking care of HTTP interactions and retrying downloads after failures or across connectivity changes and system reboots.
But, download manager never resumes download, after network connection is restored(At least, in my case). I even tried with setting the request header, using addRequestHeader(). But nothing worked. My code is as follows:
Request request = new Request(Uri.parse(BASE_URL));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle("Aarti Sangrah.zip");
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, "Aarti Sangrah.zip");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
} else {
request.setShowRunningNotification(true);
}
if (isResuming) {
request.addRequestHeader("Range", "bytes="
+ file.length());
}
enqueue = dm.enqueue(request);
In the docs, I couldn't found any method for resuming downloads. Also, I have a BroadcastReceiver for monitoring network changes.

Related

DownloadManager enqueue queue file and keeps on downloading in Oreo and above

I am using DownloadManager to download pdf file in my application. In devices with OS version of Oreo and above, the file is queued for download and keeps on downloading. But after some time nothing happens and no error or exception is shown. The same works fine in the device of OS version below Oreo.
Below is the code used.
private void startDownload(String downloadPath, String destinationPath) {
Uri uri = Uri.parse(downloadPath); // Path where you want to download file.
File destinationFile = new File(Environment.getExternalStorageDirectory() + "/folder_name");
destinationFile.mkdir();
destinationFile = new File(destinationFile.getAbsolutePath(), "file_name.pdf");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); // Tell on which network you want to download file.
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // This will show notification on top when downloading the file.
request.setTitle("Downloading a file"); // Title for notification.
request.setVisibleInDownloadsUi(true);
request.setDestinationUri(destinationFile); // Storage directory path
((DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request); // This will start downloading
}
Have enabled required permissions and tried setting Network type as well but no luck. Please help me with this.
Thanks in advance.

Android DownloadManager loading while downloading with api 9

My app has 5 big text files on them, each one about 40kb. Those files are updated weekly on a website.
I managed to set downloadManager to download the 5 files from the site. The problem is that I don't want the user to be able to use the app while the files are downloading because that will cause problems.
I want that when the user clicks the "Update" button it will show up a loading box. Then when the files finish downloading the box will disappear and a toast will say "Updated."
This is the method I've set up:
#SuppressLint("NewApi")
public void DownloadTxtFile(String url, String fileName) {
if (!new File(Environment.getExternalStorageDirectory()
+ "/folder").exists()) {
new File(Environment.getExternalStorageDirectory()
+ "/Dovahzul_Dictionary").mkdirs();
}
File file = new File(extStore.getAbsolutePath()
+ "/folder/" + fileName);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.setDescription("Downloading Text Files...");
request.setTitle("Updating")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir("/Dovahzul_Dictionary",
fileName);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
if (file.exists()) {
file.delete();
} else {
}
manager.enqueue(request);
}
By the way, if you find any problem with the method, plase inform me as well.
Thanks in advance.
I know this is old, but to anyone that has the same problem: What you need to do, is start the download in a different thread.
As android is not thread prof, you need to use AsyncTask instead. An AsyncTask is very similar to a thread. By starting and using one, you can do some quite long operations without freezing the ui. (Note that AsyncTasks are recomended for operations that take seconds, not minutes. In that case, you need to find something else)
You start an AsyncTask, and make it download the file. The problem is that AsyncTasks can't access ui methods, so they can't change threads.
You can, luckly, override a method they have to update the ui, and call it directly from it's onExecute.
Many tutorials on this can be found in the web.

DownloadManager download "stopping" during the download

I am attempting a download through the DownloadManager. It is calling just fine and seems to initialize just fine. I can see a file is created in my "music" directory and it even starts to grow a bit. Once it gets to around 3-4 MB then the download will cancel and say "download" unsuccessful. This might be because I am downloading from a slow source but it is definitely available. Any ideas on how to not have this quit/timeout. Below is the function that i am using.
In case it is important. I am calling this from an Asynchronous method and I am looping thorough a map that will begin the download of 20 (or so) mp3 files.
Thanks,
Craig
private void download(String strUrl, String fileName){
try {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(strUrl));
request.setDescription(strUrl);
request.setTitle(fileName);request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC,fileName);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
} catch (Exception e) {
e.printStackTrace();
Log.i("ERROR: ", e.getCause().getMessage());
}
}
FWIW - I never found a good answer to this question but once I fixed my network it seemed to go away. It still occassionally happens but it is relatively infrequent and appears to be network related.

How to set an icon for the DownloadManager Request?

I'm trying to set an icon for the DownloadManager request that I created, but I can't manage to do that.
I read about creating a BroadcastReceiver, but I was wondering if it's possible to do that without it.
This is my code for the DownloadManager Request:
final String filename = getIntent().getExtras().getString("id") + ".apk";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(filename);
request.setDescription("Downloading...");
request.setMimeType("application/vnd.android.package-archive");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir("/Library", filename);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Toast.makeText(LinkActivity.this, "Download has started.", Toast.LENGTH_LONG).show();
Here's an example of what it looks like now:
My target is to change the image of the notification ImageView on the left.
Thank you very much.
You cannot override this icon. And, if I remember correctly, it would make no much sense to be able to, as download manager can have more than one downloading running and it will still take one notification slot to indicate that...

Message from DownloadManager in closed NotificationBar in Android

I am using DownloadManager to download a xml file from a URL. It works fine but I have two questions:
1.) How can I show a message about the download in the closed notification bar? I can show a message when I open the bar like shown in this snapshot:
2.) How can I programmatically remove tis notification?
My code for the DownloadManager:
//Download XML file from URL
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
request.setTitle("Download von "+Name+".xml");
request.setDescription("Download von "+Name+".xml");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(FileSeperator+"XML"+FileSeperator, Name + FileExtension);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
The DownloadManager API provides a remove(long... ids) method that lets you delete the downloaded file from the filesystem. remove() deletes the file from both the filesystem and the Download Manager app's list of downloaded files. It also removes the progress notification from the system notification drawer. Just pass it the ID you received when you enqueued your request.
What might be a bit surprising is that there may also be a completion notification, and if your download completes successfully, DownloadManager does not delete the completion notification, even if you call remove() with your download request ID.
Even if the user deletes the file from the Download Manager app, it will also remove only the progress notification from the system notification drawer, but the completion notification still persists. And if the user has removed the app manually, or if you have removed the download programmatically (via remove()), then the notification causes a crash in the OS, with a report of "Unfortunately, the process android.process.media has stopped." This seems to be an Android bug.
Even the NotificationManager API doesn't help because DownloadManager owns the completion notification, not your app. So cancelAll() won't have any effect.
Bottom line: if you don't want the completion notification, don't call setNotificationVisibility() on your request with the value VISIBILITY_VISIBLE_NOTIFY_COMPLETED. The default visibility is VISIBILITY_VISIBLE, which only displays the progress notification. And as I've described, that progress notification goes away automatically.
If you want total control over notifications, you can set your request to use VISIBILITY_HIDDEN (which requires the permission android.permission.DOWNLOAD_WITHOUT_NOTIFICATION). And then you can display whatever custom notifications you'd like in the usual way with the NotificationManager API.
For my app, where I'm downloading a jar file and then extracting its contents and removing the jar file, I settled on VISIBILITY_VISIBLE (with no completion notification). Here's how I create the request:
Request request = new Request(baseUrl + destinationFile))
.setDescription("file description")
.setDestinationInExternalFilesDir(mContext, null, destinationFile)
.setNotificationVisibility(Request.VISIBILITY_VISIBLE)
.setVisibleInDownloadsUi(true)
.setTitle("My App Title");
Yes, you can show a notification when the download is complete. You can remove it too. Follow this post How can I programmatically open/close notifications in Android?
You can use following function at onReceive method of broadcastReceiver
downloadManager.addCompletedDownload(YOUR_TITLE, YOUR_DESCRIPTION, true, YOUR_MIMETYPE, downloadedFile.getPath(), downloadedFile.getTotalSpace(), false);
BroadcastReceiver like this:
registerReceiver(broadcastReceiver, new IntentFilter("android.intent.action.DOWNLOAD_COMPLETE"));
If you set the last parameter to "true" the notification will be shown if download is complete.
If set to "false" the notification is only displayed while downloading and if download is complete and notification is canceled automatically
Notification overview. Method notify. Method cancel.

Categories

Resources