Android - DownloadManager - clear old queued downloads

I am creating an application that should know for concurrency reasons if all downloads are complete. A certain function is allowed to run only after all my downloads have been completed.

I managed to write a function that checks the queue for old downloads:

DownloadManager dm = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE); Query q = new Query(); q.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING); Cursor c = dm.query(q); 

The problem is that, to be sure - during initialization, I want to clear the queue and delete all entries.

Any ideas how I can delete entries now?

This function does not work for me because I do not want to physically delete files ... just an empty queue.

Any ideas?

+4
source share
2 answers

Can you wait for DownloadManager to complete only your own downloads? To achieve this, you can save your download descriptors with the following:

 List<Long> downloadIds = new ArrayList<>(); downloadIds.add(downloadManager.enqueue(request)); 

Then you can request downloadManager as follows:

  Query q = new Query(); long[] ids = new long[downloadIds.size()]; int i = 0; for (Long id: downloadIds) ids[i++] = id; q.setFilterById(ids); Cursor c = downloadManager.query(q); Set<Long> res = new HashSet<>(); int columnStatus = c.getColumnIndex(DownloadManager.COLUMN_STATUS); while (c.moveToNext()) { int status = c.getInt(columnStatus); if (status != DownloadManager.STATUS_FAILED && status != DownloadManager.STATUS_SUCCESSFUL) { // There is at least one download in progress } } c.close(); 

Keep in mind that this can work if you are only interested in your own downloads, and not any possible downloads that might happen on the system.

Hope this helps.

+1
source

If you do not want to physically delete the files, you can back them up before calling the remove DownloadManager function. This is not an elegant solution, but I can not find other ways.

0
source

All Articles