Search code examples
androidbroadcastreceiverlocalbroadcastmanager

How to pass a handler to LocalBroadcastManager.registerReceiver(...)


Android provides a global registerReceiver() with a signature that takes a Handler. When using LocalBroadcastManager.registerReceiver(), there is no method that takes a Handler. Why? How can I provide a Handler which will process the broadcast?


Solution

  • When using LocalBroadcastManager.registerReceiver(), there is no method that takes a Handler. Why?

    Why would there be? There's no method that takes a HashMap or a Restaurant either.

    How can I provide a Handler which will process the broadcast?

    Your BroadcastReceiver instance can hold a Handler in a data member, on which it can forward the message.

    Of course, in that case, there's little value in using LocalBroadcastManager in the first place. Just use the Handler (and perhaps a Messenger). Or, switch to an event bus that offers more threading flexibility, like greenrobot's EventBus, so you can get rid of the Handler.