I'm trying to receive broadcasts from a service with 2 different receivers. One receiver update's a view so I register it in the activity's onResume method.
When the app is not in the foreground I use the other receiver so I can show a system notification when the background service completes.
The code below is how I'm registering my receivers:
@Override
protected void onPause() {
// unregister local
unregisterReceiver(localReceiver);
// register remote
registerReceiver(remoteReceiver, filter);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
// remove remote receiver
// since remote is only registered in onPause it won't be registered during the first onResume call
// so we want to ignore any exceptions
try {
unregisterReceiver(remoteReceiver);
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, "No receiver registered, could be first time");
}
// add local receiver
registerReceiver(localReceiver, filter);
Log.i(LOG_TAG, "resumed. should be registered");
}
The two receiver's are instantiated like this at the top of the Activity class:
BroadcastReceiver localReceiver = new BroadcastReceiver() { ... };
WaitTimeReceiver remoteReceiver = new WaitTimeReceiver();
The Service makes the intent as:
broadcastIntent = new Intent(Support.SERVICE_BR);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
// later on sends using
sendBroadcast(broadcastIntent);
The Filter in the Activity is made to match:
filter.addAction(Support.SERVICE_BR);
filter.addCategory(Intent.CATEGORY_DEFAULT);
Everything above is working the pause resume functionality works as expected, but my question is why the LocalBroadcastManager was not?
Using LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this)
and then calling lbm.registerReceiver(localReceiver)
was not receiving any of my broadcasts for either.
Why wasn't the LocalBroadcastManager receiving any of my broadcasts?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…