Is there a way to tell if LocalBroadcastManager broadcasts were received? Or are being listened to?
Basically I have an IntentService listening for Google Cloud Messages. When it gets one I need to either show a notification OR alert my main Service that there's been a new message - crucially I don't want both! So I need to know whether the message was dealt with by my main Service ...
Clearly it can be done with sendOrderedBroadcast and a BroadcastReceiver but that seems overkill for my simple private intra-process needs.
But event buses suffer from the same problems that LocalBroadcastManager does that led to it being deprecated: it's global, it's not lifecycle-aware, and as your app gets larger, it becomes much more difficult to reason about the effects of a change to an event.
LocalBroadcastManager is used to register and send a broadcast of intents to local objects in your process. It has lots of advantages: You broadcasting data will not leave your app. So, if there is some leakage in your app then you need not worry about that.
Broadcast receiver is an Android component which allows you to send or receive Android system or application events. All the registered application are notified by the Android runtime once event happens. It works similar to the publish-subscribe design pattern and used for asynchronous inter-process communication.
Is there a way to tell if LocalBroadcastManager broadcasts were received? Or are being listened to?
sendBroadcast()
returns true
if there was 1+ receivers, false
otherwise. That's not documented, but that is based on the current implementation. I filed an issue to get that documented.
Hence, your IntentService
could use sendBroadcast()
to try to send a message to your running Service
, if it exists. If it does, sendBroadcast()
should return true
, and the IntentService
knows that the message should be handled there. If sendBroadcast()
returns false
, you can raise the Notification
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With