0

DownloadManager is a background service. I would like to get a list of files that are currently being downloaded using DownloadManager.

Say download process is launched in activity A and I open activity B. In activity B I would like to know which url/file is being downloaded. How to achieve this? If multiple files are being downloaded, then how can I get the list?

suku
  • 10,507
  • 16
  • 75
  • 120

1 Answers1

0

You can do something like below in your broadcast receiver to identify the files that ate downloaded. Replace YOUR_DM with your DownloadManager instance.

receiver_complete = new BroadcastReceiver(){
         @Override
          public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
                if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE){
                    Bundle extras = intent.getExtras();
                    DownloadManager.Query q = new DownloadManager.Query();
                    q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
                    Cursor c = YOUR_DM.query(q);

                    if (c.moveToFirst()) {
                    int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
                    if (status == DownloadManager.STATUS_SUCCESSFUL) {
                    // process download
                    title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
                    // get other required data by changing the constant passed to getColumnIndex
                    }
                }
             }
         }
     };
Akanksha Hegde
  • 1,738
  • 11
  • 14
  • I need list of files that are currently being downloaded. Hence, by this code I need to send a broadcast when file is starting to download and has finished download and maintain a list of the started and completed download – suku Jan 11 '17 at 07:20
  • In document, https://developer.android.com/reference/android/app/DownloadManager.html you'll get list of all the constants you want with the download manager. So if you want onging downloads also you can get it. – Akanksha Hegde Jan 11 '17 at 07:27
  • I shouldn't need to broadcast to find ongoing downloads as it is not an event – suku Jan 11 '17 at 07:32