Android Download File With DownloadManager and Check Status (Java)
April 15, 2019Request a download
- Specify a url
- Optional: Specify a name (the name is shown in Android Notification, if you enable it)
- You can specifiy to perform download on Wifi or Mobile or both
- Specify the download location and file name
- Save the
Download ID
to check on status later
String url = ...
String name = ...
String downloadFileName = ...
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(item.downloadUrl);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setTitle(name);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, downloadFileName);
long downloadId = downloadManager.enqueue(request);
Check download status by Download ID
long downloadId = ...
Cursor cursor = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId));
if (cursor != null && cursor.moveToNext()) {
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
cursor.close();
if (status == DownloadManager.STATUS_FAILED) {
// do something when failed
}
else if (status == DownloadManager.STATUS_PENDING || status == DownloadManager.STATUS_PAUSED) {
// do something pending or paused
}
else if (status == DownloadManager.STATUS_SUCCESSFUL) {
// do something when successful
}
else if (status == DownloadManager.STATUS_RUNNING) {
// do something when running
}
}
Download might not start immediately (pending), so you can run a timer to check for status to make sure it is running.
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
int status = ... // check status
if (status == DownloadManager.STATUS_RUNNING || status == DownloadManager.STATUS_SUCCESSFUL) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// update UI
}
});
timer.cancel();
}
}
}, 1000);
Get a callback/notification when download is successful or cancelled.
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
DownloadReceiver downloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (downloadId == -1)
return;
// query download status
Cursor cursor = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId));
if (cursor.moveToFirst()) {
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
if(status == DownloadManager.STATUS_SUCCESSFUL){
// download is successful
String uri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
File file = new File(Uri.parse(uri).getPath());
}
else {
// download is cancelled
}
}
else {
// download is cancelled
}
}
};
registerReceiver(downloadReceiver, filter);
NOTE: Refer Android DownloadManager Listen to Download Complete and Cancel event/action
NOTE: You might want to implement a broadcast receiver for DownloadManager.ACTION_NOTIFICATION_CLICKED
to appropriately handle when the user clicks on a running download in a notification or from the downloads UI.
- android
- android-ktx
- android-studio
- apps-script
- bootstrap
- cloud-functions
- coding-interview
- coroutines
- crashlytics
- css
- dagger2
- datastore
- datetime
- eslint
- firebase
- firebase-auth
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- google-app-engine
- google-cloud-storage
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- kotlin
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- markdown
- md5
- moshi
- navigation
- nginx
- nodejs
- npm
- payment
- pip
- python
- recylerview
- regex
- room
- rxjava
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-cli
- vuejs
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- yarn