活动中有两个下载管理器,我为同一个意图注册了两个不同的广播接收器。
问题是,没有什么比“请求代码”更好,并且两个接收器似乎重叠,有时触发第一个,有时触发第二个。
mgr = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
ctx.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
mgr.enqueue(request);
mgr = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
ctx.registerReceiver(onImgComplete , new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
mgr.enqueue(request);
onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
pDialog.dismiss();
play();
}
};
onImgComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
pDialog.dismiss();
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("image/jpeg");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, intent_type_string +" - " + item.name);
emailIntent.putExtra(Intent.EXTRA_TEXT, intent_msg + "\nDownload EasyFind:\nhttp://yahoo.com.hk");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tmp_img));
startActivity(emailIntent);
}
};
我如何分开它们?
答案 0 :(得分:1)
您只能注册一个接收器。在它的onReceive方法中,intent参数应该有一个额外的下载ID:
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
这是开始下载时应保存的ID:
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse("YOUR_LINK"));
long queueID = dm.enqueue(request);
接下来,您可以提取有关下载文件的必要数据:
Query query = new Query();
query.setFilterById(queueID);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
String uriString = c.getString(
c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
String mediaType = c.getString(
c.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE));
//TODO IMPLEMENT
}
注意:使用的查询是DownloadManager.Query