gpt4 book ai didi

android - 为 DownloadManager 的 BroadcastReceiver 设置附加功能

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:25:31 27 4
gpt4 key购买 nike

<分区>

有一种方法可以在 DownloadManager 的 intent 中为 actionDownloadManager.ACTION_DOWNLOAD_COMPLETE 注册额外内容(例如,接收一个在 intent 中设置为 extra 的 bool 值)?

这是我创建请求的方式:

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

在我的 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));
}
}
}

使用 intent.getExtras() 我只能获取请求参数。我尝试使用不同的操作将广播发送到同一个接收器(一个使用 ACTION_DOWNLOAD_COMPLETED,另一个是自定义的),但我必须发送一个双广播,所以它会在 onReceive 中输入两次。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com