为DownloadManager的BroadcastReceiver设置额外内容

Gio*_*oli 6 android download-manager broadcastreceiver android-intent

有一种方法可以将额外内容放入DownloadManager注册操作的意图中DownloadManager.ACTION_DOWNLOAD_COMPLETE(例如,在意图中接收设置为额外的布尔值)?

这是我创建请求的方式:

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));
Run Code Online (Sandbox Code Playgroud)

在我的onComplete接收器中:

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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

随着intent.getExtras()我仅能够获得请求参数.我试图用不同的动作向同一个接收器发送广播(一个用ACTION_DOWNLOAD_COMPLETED,另一个是自定义的),但是我必须发送一个双广播,所以它会在onReceive中输入两次.

Com*_*are 7

有一种方法可以在为DownloadDownloadManager.ACTION_DOWNLOAD_COMPLETE注册的DownloadManager意图中添加额外内容(例如,在意图中接收一个设置为额外的布尔值)?

号使用你找回ID 从enqueue()储存您想要的boolean地方持久(例如,在一个文件中),所以您可以在收到您的广播读取值回.

此外,对于您的代码段,请记住,下载完成后您的进程可能不在周围.因此,您的BroadcastReceiver注册通道registerReceiver()可能永远不会被触发.