Search code examples
androidandroid-download-manager

How to check if file is in downloadmanager queue?


The user has to download multiple files which will start on a button click. The code will check if a file already exists, if not then it adds it to the download queue. I am adding all the non-existent files to the download queue.

But if the user clicks on the button again immediately, the files get re-added to the download queue. So how to check if the file is added to the download manager queue? If it is added then, the file will not be re-added.


Solution

  • you should use DownloadManager.Query

    Here is example you can refer Download manager example

    sample code

    DownloadManager.Query query = null;
    Cursor c = null;
    DownloadManager downloadManager = null;
    downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
    query = new DownloadManager.Query();
     if(query!=null) {
                query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|DownloadManager.STATUS_SUCCESSFUL|
                        DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PENDING);
            } else {
                return;
            }
    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;  
    }
    }