为 DownloadManager 的 BroadcastReceiver 设置附加功能
Set extras for DownloadManager's BroadcastReceiver
有一种方法可以在 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));
在我的 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 中输入两次。
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)?
没有。使用您返回的 ID from enqueue()
将您想要的 boolean
永久存储在某处(例如,在文件中),这样您就可以在收到广播时读回该值。
此外,关于您的代码片段,请记住您的进程可能在下载完成时不存在。因此,您通过 registerReceiver()
注册的 BroadcastReceiver
可能永远不会被触发。
答案是正确的,您不能为 DownloadManager 的意图添加额外的内容。但是您可以为 DownloadManager 的请求设置描述,然后在下载完成时阅读它。我想这对你来说已经足够了。
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();
您可以看到上面的描述字段。现在我将在 BroadcastReceiver 的 onReceive 方法中下载完成后阅读此内容。
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));
}
}
有一种方法可以在 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));
在我的 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 中输入两次。
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)?
没有。使用您返回的 ID from enqueue()
将您想要的 boolean
永久存储在某处(例如,在文件中),这样您就可以在收到广播时读回该值。
此外,关于您的代码片段,请记住您的进程可能在下载完成时不存在。因此,您通过 registerReceiver()
注册的 BroadcastReceiver
可能永远不会被触发。
答案是正确的,您不能为 DownloadManager 的意图添加额外的内容。但是您可以为 DownloadManager 的请求设置描述,然后在下载完成时阅读它。我想这对你来说已经足够了。
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();
您可以看到上面的描述字段。现在我将在 BroadcastReceiver 的 onReceive 方法中下载完成后阅读此内容。
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));
}
}