How to pause and resume DownloadManager - android

I know this question is duplicated, I haven't got complacent answer.
I am using Android System DownloadManager to download file from net work, it works fine with downloading file.
private DownloadManager downloadManager;
Button start;
Button pause;
Button resume;
initializing all the buttons above
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri
.parse("uri");
DownloadManager.Request request = new DownloadManager.Request(
Download_Uri);
then it has been put download queue. when turn is coming it begin downloading
my problem is, is there any simple way to pause download and resume download
like
downloadManager.pause();
downloadManager.resume();
Thanks in advance.

Related

Open PDF if already downloaded, if not, download the PDF using download manager

I am creating an app that displays PDF. I want to create one button that will both download the PDF (if it does not exist) and open it (if it already exists). I am able to download a PDF but how do I proceed further?
public class Download {
DownloadManager downloadManager;
long queid;
public Download(Context context, String downloadurl, String filename){
downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadurl));
request.setDestinationInExternalFilesDir(context.getApplicationContext(), Environment.DIRECTORY_DOWNLOADS, filename);
queid = downloadManager.enqueue(request);
}
}
You have downloaded the pdf. Save it or move it to a fixed folder location on SDCard. In button click event, check if the pdf file is present on the same location. If it is present then you should open it in your pdf viewer activity in app.
You may find few libraries to enable viewing pdf in your app. One of the library which I use is AndroidPdfViewer. You can get help on how to integrate it in your app from the gitbhub link.
Regards.

Android DownloadManager how to mark file as temporary before finishing downloading

I have a DownloadManager to download video so that I can use player to play. But when I start to download, it has generated the unfinished .mp4 file, so when I tried to play before finish downloading, it will show error, is that possible change its extension before finish downloading?
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri); request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI|DownloadManager.Request.NETWORK_MOBILE).setDescription("caching video").setDestinationInExternalPublicDir("/Android/data/", id + ".mp4");
long id = manager.enqueue(request);
You can solve your problem in a little different way
Instead of change its extension before finish downloading you need to start the download with the changed extension like this:
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI|DownloadManager.Request.NETWORK_MOBILE).setDescription("caching video").setDestinationInExternalPublicDir("/Android/data/", id + ".mp4.tmp");
and once download is finished, rename file to "/Android/data/", id + ".mp4"

Download manager does not recognise .mp3 files

I am using Download Manager
and i want to download some audio files using my own webview.
I use the following code:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url2));
request.setDescription("Downloading");
request.setTitle("File :");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "audio.mp3");
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Even if the the File Name is audio.mp3 download manager does not recongise the file type.
ScreenShot explains the main problem.
Using other apps like default browser downloaded files look like this!
The solution is simple:
Just add the following line
request.setMimeType("audio/MP3");
More info here
not work : request.setMimeType("audio/MP3");
To operate, follow the steps below.
request.setMimeType("audio/mpeg");

Customizing a download notification

Based on another SO answer, I am using the following lines of code to download media files:
DownloadManager.Request r = new DownloadManager.Request(Uri.parse(uri));
// This put the download in the same Download dir the browser uses
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_PODCASTS, fileName);
// When downloading music and videos they will be listed in the player
// (Seems to be available since Honeycomb only)
r.allowScanningByMediaScanner();
// Notify user when download is completed
// (Seems to be available since Honeycomb only)
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// Start download
DownloadManager dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(r);
Now, that is all good except that I have a few questions:
The person who answered said that the notifications can be customized. How can that be done?
If I want to add a "cancel" button to cancel the download and delete the file, how do I do it? What do I need to implement to get this behavior? :)
// Changing the Notification Title, Description, and its Visibility
DownloadManager mgr = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("///.mp3");
DownloadManager.Request req = DownloadManager.Request(uri);
req.setTitle("Download");
req.setDescription("Kick Ass Description");
req.setVisibility(Request.VISIBILITY_VISIBLE_COMPLETION_ONLY);
Pretty self-explanatory.

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...

Categories

Resources