简体   繁体   中英

How to find name of file being downloaded using Download Manager

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 BI 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?

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
                    }
                }
             }
         }
     };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM