8

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.

ostergaard
  • 3,377
  • 2
  • 30
  • 40
  • Why do you need two services in the first place? – CommonsWare Sep 01 '13 at 13:44
  • 3
    @CommonsWare - because they have massively different purposes and lifecycles and it therefore improves my application architecture to separate them. One is an IntentService and the other is a bound service. – ostergaard Sep 01 '13 at 14:00

1 Answers1

29

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491