androidlocalbroadcastmanager

can a receiver be registered with LocalBroadcastmanager to execute in a different thread


Can a receiver be registered with LocalBroadcastManager to execute in a different thread? We register for an intent with LocalBroadcastmanager using

void registerReceiver (BroadcastReceiver receiver, IntentFilter filter)

Suppose I want the onReceive method to be called on a different thread other than the main thread then how do I achieve it?

I don't see any API like

Intent registerReceiver (BroadcastReceiver receiver, 
            IntentFilter filter, 
            String broadcastPermission, 
            Handler scheduler)

Solution

  • I have forked LocalBroadcastManager and enhanced it to be able broadcast on an arbitrary java thread. Essentially I keep a map of Handers derived from a new Looper param in the registerReceiver method. The handler is invoked when it is time to broadcast. Synchronous behavior is unchanged. I have used it extensively but would appreciate any constructive criticism. The code is here:

    public void registerReceiver(BroadcastReceiver receiver, IntentFilter filter, Looper looper)

    https://github.com/sorenoid/LocalBroadcastManager

    I hope it is helpful.