-
Notifications
You must be signed in to change notification settings - Fork 12
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));
- ACTION_DOWNLOAD_COMPLETE is a broadcast sent by DownloadManager when it is done with your download.Since many apps can be downloading simultaneously you have to check and see it if the id of the download is your ID. Note that we have saved the download ID in SharedPrefs for later reference.
- More info : https://developer.android.com/reference/android/app/DownloadManager.html
- Relevant files:
- Direct link to the app: