0

The Intents I broadcast with LocalBroadcastManager don't seem to be consumed, but instead accumulate. Each time I send a broadcast, I receive both it and all previously sent broadcasts, in onReceive.

In a periodical JobService I'm creating a BroadcastReceiver with an IntentFilter and registering it dynamically with lbm.registerReceiver(receiver, intentFilter);. I then start a Service which creates an Intent with the same filter in its constructor, and does lbm.sendBroadcast(intent); once, then stops itself. sendBroadcast returns true which should indicate that it was received on the other end (see Is there a way to tell if LocalBroadcastManager broadcasts were received?). However each time this cycle is repeated, all the previously received Intents are received again. By bundling a timestamp I have confirmed that it's really the old ones just lying around and being received over and over again.

How do I "consume" the received Intent? From what I can tell this shouldn't be necessary if it was received by onReceive. I don't find any methods for cancelling a broadcast in either Intent or LocalBroadcastManager.

How can I receive each broadcast only once?

Community
  • 1
  • 1
Toerndev
  • 715
  • 1
  • 6
  • 21
  • 1
    Make sure to unregister your BroadCastReceiver when the JobService is done. – Talha Mir May 31 '16 at 08:52
  • I had indeed forgotten to do so before jobFinished in one place. Results are looking better so far. A ray of hope?! Thanks for the tip! – Toerndev May 31 '16 at 09:17

1 Answers1

0

Thanks to @Talha Mir who posted the solution as a comment.

I was only calling unregisterReceiver in onStopJob, not in other paths. Adding this fixed the problem.

Apparently a broadcast can stick around after onReceive, if the receiver isn't unregistered.

Toerndev
  • 715
  • 1
  • 6
  • 21