Search code examples
androiddownload-manager

Downloadmanager request status change notification


I haven't found any info about that. Is it somehow possible listen for downloadmanager request status changes? I queue few files for download and I display it in list with download state. I receive broadcast only on DownloadManager.ACTION_DOWNLOAD_COMPLETE but I would like to receive some notification or set some listener so I will be able to track download request changes. I want display state - added to queue, downloading, downloaded...

Only way which I see is query downloadmanager and check every request about state, but I need to do it in listadapter getview method and it will be not very efficient.


Solution

  • According to the official documentation there isn't a good way of doing this. http://developer.android.com/reference/android/app/DownloadManager.html

    I would recommend either starting a service or some sort of background thread that queries for the status of just your downloads every couple of seconds. (keep track of their ids in a long[], because setFilterById takes a long[])

    long[] ids = new long[MyDownloadQueue.length];
    // Look at all these requests I'm building!
    ids[i++] = dm.enqueue(request);
    
    Cursor cursor = dm.query(new DownloadManager.Query().setFilterById(ids));
    while (cursor.moveToNext()) {
      int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
      int status = cursor.getInt(columnIndex);
      int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
      int reason = cursor.getInt(columnReason);
      // your stuff here, recording the status and reason to some in memory map
    }
    

    This way your listview won't have to do any of the work and the UI will stay smooth. There isn't really a better way that I know of however, sorry I couldn't provide an answer that didn't involve querying.