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) {
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.
source share