Search code examples
androidbroadcastreceiverandroid-download-manager

Receive download manager download complete intent and compare id - android


I am using download manager in my activity to download a file. I want to receive the ACTION_DOWNLOAD_COMPLETE intent using a broadcast receiver and match the id of the download request with the id in the intent set by download manager to check if it is the same download.

There are two options:

1) receive the intent in the activity by making a intent filter and registering a broadcast receiver. Problem here is that the broadcast receiver exists within the activity and gets destroyed with it. So if the download doesn't get completed before the activity is destroyed there is no way to receive the intent.

this.downloadId = manager.enqueue(request);

downloadID is compared with the EXTRA_DOWNLOAD_ID of the intent in onReceive method to check if it is the same download. downloadId variable is accessible inside the onReceive method.

2) receive the broadcast via manifest. Problem here is that the onReceive method does not have access to the downloadId variable set inside the activity. So I cannot check if the download is the one I need.

Am I doing it wrong? Basically I want to know if the download was completed successfully or not even if the activity/service where the download started is destroyed.

I hope my question is clear enough.


Solution

  • surely your first option is not good because you must do some work as soon as your download is completed so i suggest you use the second approach and store the downloadid in sharedpreferance or in a file and in onRecieve method check that by stored value. you can also use database but if you want to store just one variable (downloadid) in it, it is not a good idea because you must create table and ... so let's see some code:

    in your activity after saving your downloadid to a variable put it in a SharedPreferences file like:

    SharedPreferences settings = getSharedPreferences("DownloadIDS", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putLong("savedDownloadIds", your download id);
    editor.commit();
    

    and in onReceive:

    @Override
    public void onReceive(Context context, Intent intent) {
    
        SharedPreferences downloadids = context.getSharedPreferences("DownloadIDS", 0);
        long savedDownloadIds = downloadids.getLong("savedDownloadIds", 0);
    
        Bundle extras = intent.getExtras();
        DownloadManager.Query q = new DownloadManager.Query();
        Long downloaded_id = extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
        if(savedDownloadIds == downloaded_id ){ // so it is my file that has been completed
        q.setFilterById(downloaded_id);
        DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        Cursor c = manager.query(q);
        if (c.moveToFirst()) {
            int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
            if (status == DownloadManager.STATUS_SUCCESSFUL) {
               // do any thing here
            }
        }
        c.close();
       }
      }