Set extras for DownloadManager's BroadcastReceiver [duplicate]

♀尐吖头ヾ 提交于 2019-12-08 20:19:17

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/32309637/set-extras-for-downloadmanagers-broadcastreceiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!