Skip to content

Use DownloadManager to download large files

rutura edited this page Apr 16, 2017 · 1 revision
  • MinSdk : 9
  • Background download large files
  • DownloadManager has advantages of being managed by the system and your connection can survive configuration changes of all kinds even resume after device reboot.
  • Initialize the download manager somewhere convenient:
   dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
  • And fire off the download somewhere, done it onResume override method in this example:
    Uri resource = Uri.parse(
                    "http://www.blikoon.com/salama_mobile_app/android/release/Salama.apk");
            DownloadManager.Request request =
                    new DownloadManager.Request(resource);
            //Set allowed connections to process download
            request.setAllowedNetworkTypes(
                    DownloadManager.Request.NETWORK_MOBILE
                            | DownloadManager.Request.NETWORK_WIFI);
            request.setAllowedOverRoaming(false);
            //Display in the notification bar
            request.setTitle("Salama Download");
            //Retrieve the download ID.This will be used when we resume the connection.
            long id = dm.enqueue(request);
            //Save the unique id
            prefs.edit().putLong(DL_ID, id).commit()
  • Set up a broadcast receiver so you are notified when the download is done:
    registerReceiver(receiver, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Clone this wiki locally