Why is this cursor empty?

I have a button, and when the user clicks on it, I want to find out the download history using DownloadManager, but the problem is that my cursor is empty, it never goes into the if (c.moveToFirst ()) state, it always skip this condition.

DownloadManager downloadMgr = (DownloadManager)getSystemService(DOWNLOAD_SERVICE); DownloadManager.Query query = new DownloadManager.Query(); query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_SUCCESSFUL); Cursor c = downloadMgr.query(query); if(c==null) { // } else { if(c.moveToFirst()) { int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS); int status = c.getInt(columnIndex); if(status == DownloadManager.STATUS_RUNNING){ //do something } } } 

I also tried this code (by imran khan), but still the same problem:

 DownloadManager.Query query = null; Cursor c = null; DownloadManager downloadManager = null; downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE); query = new DownloadManager.Query(); query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_SUCCESSFUL); c = downloadManager.query(query); if(c.moveToFirst()) { int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); switch(status) { case DownloadManager.STATUS_PAUSED: break; case DownloadManager.STATUS_PENDING: break; case DownloadManager.STATUS_RUNNING: break; case DownloadManager.STATUS_SUCCESSFUL: break; case DownloadManager.STATUS_FAILED: break; } } 
+6
source share

Source: https://habr.com/ru/post/913837/


All Articles