Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to use download manager class?

I want to download a binary file from a url. Is it possible to use the Android download manager class that I found here DownloadManager class?

like image 345
Kris Avatar asked May 04 '11 02:05

Kris


People also ask

How do I use download manager on Android?

Once you are sure DownloadManager is available, you can do something like this: String url = "url you want to download"; DownloadManager. Request request = new DownloadManager. Request(Uri.

Do I need download manager on my Android?

If you're usually on an erratic mobile network, the chances of this happening are even higher. Hence, you need a download manager. Download managers can help you overcome several common hassles about downloading from the internet.


2 Answers

Is it possible to use the android download manager class that i found here

Yes, though that is only available since Android API Level 9 (version 2.3). Here is a sample project demonstrating the use of DownloadManager.

like image 78
CommonsWare Avatar answered Sep 23 '22 13:09

CommonsWare


Use DownloadManager class (GingerBread and newer only)

GingerBread brought a new feature, DownloadManager, which allows you to download files easily and delegate the hard work of handling threads, streams, etc. to the system.

First, let's see a utility method:

/**  * @param context used to check the device version and DownloadManager information  * @return true if the download manager is available  */ public static boolean isDownloadManagerAvailable(Context context) {      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {         return true;     }     return false; } 

Method's name explains it all. Once you are sure DownloadManager is available, you can do something like this:

String url = "url you want to download"; DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setDescription("Some descrition"); request.setTitle("Some title"); // 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(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext");  // get download service and enqueue file DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(request); 

Download progress will be showing in the notification bar.

like image 40
m.chorakchi Avatar answered Sep 22 '22 13:09

m.chorakchi