I would like to check if a download is currently running. I am using the code bellow:
public static boolean isDownloading(Context context){
DownloadManager.Query query = null;
Cursor c = null;
DownloadManager downloadManager = null;
downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
query = new DownloadManager.Query();
if(query!=null) {
//return true;
query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|DownloadManager.STATUS_SUCCESSFUL|
DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PENDING);
} else {
Log.i("AUTOMATION_DOW" , "NO ");
return false;
}
c = downloadManager.query(query);
if(c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
switch(status) {
case DownloadManager.STATUS_PAUSED:
Log.i("AUTOMATION_DOWNLOAD","PAUSED");
break;
case DownloadManager.STATUS_PENDING:
Log.i("AUTOMATION_DOWNLOAD","PENDING");
break;
case DownloadManager.STATUS_RUNNING:
Log.i("AUTOMATION_DOWNLOAD","RUNNING");
break;
case DownloadManager.STATUS_SUCCESSFUL:
Log.i("AUTOMATION_DOWNLOAD","SUCCESSFUL");
break;
case DownloadManager.STATUS_FAILED:
Log.i("AUTOMATION_DOWNLOAD","FAILED");
break;
}
}
Log.i("AUTOMATION_DOWNLOAD","DEFAULT");
c.close();
return true;
}
I have the following logcat even though a download is running : 09-04 09:39:42.381 30213-31215/com.bytel.velizy.automation I/AUTOMATION_DOWNLOAD: DEFAULT
I have tried to reduce the previous code into this but it did not work.
DownloadManager.Query query = null;
Cursor c = null;
DownloadManager downloadManager = null;
downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
query = new DownloadManager.Query();
if(query!=null) {
return true;
} else {
return false;
}
If you want to get Status of a specific download In that case when you start your download at that time downloadManager.enqueue() method returns downloadId which is unique for each download so you can save this and use to get DownloadStatus like this
Here start download return you downloadId
public long startDownload(String downloadurl) {
DownloadManager downloadManager =
(DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
try {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadurl));
// enqueue method retuns downloadId
return downloadManager.enqueue(request);
} catch (Exception e) {
Log.d("DOWNLOADED_INFO", "startDownload =" + e.getMessage());
e.printStackTrace();
}
return 0;
}
This downloadId passed to getStatus method
public static int getStatus(Context context , long downloadId) {
DownloadManager downloadManager =
(DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);// filter your download bu download Id
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
c.close();
Log.i("DOWNLOAD_STATUS", String.valueOf(status));
return status;
}
Log.i("AUTOMATION_DOWNLOAD", "DEFAULT");
return -1;
}
To check If current file is downloadng
public static boolean isDownloading(Context context , long downloadId){
return getStatus(context , downloadId) == com.mozillaonline.providers.DownloadManager.STATUS_RUNNING;
}
Above method is for checking the status of a particular download. If you want to check the status of your download manager that any file is downloading or in paused status you can check by this method
public static boolean checkStatus(Context context , int status) {
DownloadManager downloadManager = (DownloadManager)
context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(status);
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
c.close();
Log.i("DOWNLOAD_STATUS", String.valueOf(status));
return true;
}
Log.i("AUTOMATION_DOWNLOAD", "DEFAULT");
return false;
}
and then simply call this method
checkStatus(context , DownloadManager.STATUS_RUNNING);