Set extras for DownloadManager's BroadcastReceiver [duplicate]
Asked Answered
G

2

6

There's a way to put extras in DownloadManager's intent registered for actionDownloadManager.ACTION_DOWNLOAD_COMPLETE (e.g. receive a boolean value set as extra in the intent)?

This is how I create the request:

DownloadManager.Request req = new DownloadManager.Request(myuri);
// set request parameters
//req.set...
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.enqueue(req);
context.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

And in my onComplete receiver:

private BroadcastReceiver onComplete = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        queryRequestParameters(context, intent);
    }
};

private void queryRequestParameters(Context context, Intent intent) {
    // get request bundle
    Bundle extras = intent.getExtras();
    DownloadManager.Query q = new DownloadManager.Query();
    q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
    Cursor c = ((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE)).query(q);
    //get request parameters
    if (c.moveToFirst()) {
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
        if (status == DownloadManager.STATUS_SUCCESSFUL) {
            // find path in column local filename
            String path = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
        }
    }
}

With intent.getExtras() I can obtain only request parameters. I tried to send Broadcast to same receiver with different actions (one with ACTION_DOWNLOAD_COMPLETED, the other is custom), but I have to send a double broadcast so it will enter two times in the onReceive.

Gelding answered 31/8, 2015 at 10:56 Comment(0)
O
7

There's a way to put extras in DownloadManager's intent registered for actionDownloadManager.ACTION_DOWNLOAD_COMPLETE (e.g. receive a boolean value set as extra in the intent)?

No. Use the ID you get back from enqueue() to store your desired boolean somewhere persistent (e.g., in a file), so you can read that value back in when you receive your broadcast.

Also, with respect to your code snippet, bear in mind that your process may not be around by the time the download is completed. Your BroadcastReceiver registered via registerReceiver(), therefore, may never get triggered.

Ottilie answered 31/8, 2015 at 11:59 Comment(4)
I understood the first part about my question, but not the second. What does mean that my process may not be around by the time the download is completed?Gelding
@Fondesa: Downloads take time. Your process will not live forever. It may not live long enough for the download to complete. If you want to find out about downloads that complete after your process has been terminated, you will need to do something else, other than what you are doing with registerReceiver().Ottilie
Oh, now I understand, thanks!Gelding
You have already answered here the same , unlucky it didn't get accepted but anyways this Question can be marked as duplicate so did it.Ecphonesis
A
3

The answer is right you can't put extras to DownloadManager's intent. But you can set description to the DownloadManager's request and then read this while download is finished. I think that will be enough for you.

    DownloadManager dm = (DownloadManager) getSystemService(BaseActivity.DOWNLOAD_SERVICE);
    DownloadManager.Request request = new DownloadManager.Request(
            Uri.parse((Constants.ROOT_URL_1 + fileName))); 
    request.setAllowedNetworkTypes(
            DownloadManager.Request.NETWORK_WIFI
                    | DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false).setTitle(title)
            .setDescription("This is what you need!!!")
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
            .setDestinationInExternalPublicDir("/my_folder", title)
            .allowScanningByMediaScanner();

You can see the decription field above. Now i am going to read this after download is finished in the onReceive method of BroadcastReceiver.

            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(downloadId);
            Cursor c = ((DownloadManager) getSystemService(BaseActivity.DOWNLOAD_SERVICE)).query(query);
            if (c.moveToFirst()) {
                int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                    String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                    String description = c.getString(c.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION));
                }
             }
Aragonite answered 29/3, 2016 at 15:30 Comment(3)
whats downloadId in your code?Gregson
@Gregson Its for filtering downloads, apparently i used DownloadManager not only for one task. You may not need it.Aragonite
@OguzOzcan But description is visible to the user in the notification!Notwithstanding

© 2022 - 2024 — McMap. All rights reserved.