Search code examples
androidandroid-download-manager

STATUS_PAUSED won't call onReceive() in DownloadManager


I used DownloadManager to download a file from server, I expect when the network is not connected to internet I receive STATUS_PAUSED in BroadcastReceiver. But it doesn't call onReceive().

downloadReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
       // ...
    }
}

registerReceiver(downloadReceiver,
    new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

Solution

  • You're filtering for the ACTION_DOWNLOAD_COMPLETE action, your receiver will not receive any other broadcasts.

    Moreover, STATUS_PAUSED is not a broadcast.

    It's the status of a particular download managed by the DownloadManager, which you can query.

    For example:

    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    DownloadManager.Query query = new DownloadManager.Query();
    
    query.setFilterById(idsToQuery);
    query.setFilterByStatus(DownloadManager.STATUS_PAUSED);
    
    Cursor cursor = dm.query(query);
    
    if (cursor.moveToFirst()) {
        // do whatever you would like with the result
    }