[英]onReceive not being called when BroadcastReceiver is registered using LocalBroadcastManager
I've created a BroadcastReceiver
to receive ACTION_DOWNLOAD_COMPLETE
when my app starts downloading something using DownloadManager
. 当我的应用程序开始使用
DownloadManager
时,我创建了一个BroadcastReceiver
来接收ACTION_DOWNLOAD_COMPLETE
。 As I want to capture ACTION_DOWNLOAD_COMPLETE
only when downloading is started from my app, I've used LocalBroadcastManager
. 因为我只想在从我的应用程序开始下载时捕获
ACTION_DOWNLOAD_COMPLETE
,所以我使用了LocalBroadcastManager
。
But onReceiver
is not being called at all. 但
onReceiver
根本没有被调用。 DownloadManager
app shows that download is complete but onReceive
is not triggered. DownloadManager
应用程序显示下载已完成但未触发onReceive
。 When I use registerReceiver
it works as expected. 当我使用
registerReceiver
它按预期工作。 But this would let app being notified even if downloading is started by some other app. 但即使下载是由其他应用程序启动的,这也会让应用程序得到通知。 So LocalBroadcastManager is desired.
所以需要LocalBroadcastManager。
MainActivity 主要活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadReceiver = new DownloadReceiver();
LocalBroadcastManager.getInstance(this).registerReceiver(downloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
if(FileHelper.isStorageAvailable()) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://example.com/image.jpg"));
downloadManager.enqueue(request);
}
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(downloadReceiver);
super.onPause();
}
DownloadReciever DownloadReciever
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
Toast.makeText(context, title, Toast.LENGTH_SHORT).show();
}
}
}
}
It simply don't call onRecieve
as it should. 它根本不应该调用
onRecieve
。 Point out if I'm doing something wrong here. 如果我在这里做错了,请指出。 Been stuck here for quite time now.
被困在这里很长一段时间了。 I can't use
registerReceiver
as I need to track download complete action only if my app starts downloading. 我不能使用
registerReceiver
因为我只有在我的应用程序开始下载时才需要跟踪下载完成操作。
As I want to capture ACTION_DOWNLOAD_COMPLETE only when downloading is started from my app, I've used LocalBroadcastManager.
因为我只想在从我的应用程序开始下载时捕获ACTION_DOWNLOAD_COMPLETE,所以我使用了LocalBroadcastManager。
That is not going to work. 那不行。
DownloadManager
does the download in a separate process, and it will use a system broadcast. DownloadManager
在单独的过程中进行下载,它将使用系统广播。 The only broadcasts that you can receive via LocalBraodcastManager
are the ones that you broadcast via LocalBroadcastManager
. 您可以通过收到的唯一的广播
LocalBraodcastManager
是您通过广播的那些LocalBroadcastManager
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.