What is more efficient Broadcast Receiver or Handler? What is more efficient Broadcast Receiver or Handler? multithreading multithreading

What is more efficient Broadcast Receiver or Handler?


You should not use normal broadcasts to communicate between Activities and Services inside of your own app. You should use local broadcasts instead! First you have to define a BroadcastReceiver like for normal broadcasts:

private static final String ACTION_EXAMPLE = "ACTION_EXAMPLE";private final BroadcastReceiver receiver = new BroadcastReceiver() {    @Override    public void onReceive(Context context, Intent intent) {        if(ACTION_EXAMPLE.equals(intent.getAction())) {            ...        }      }};

After that you can get the LocalBroadcastManager like this:

LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());

And you can register the BroadcastReceiver like this (normally you register a BroadcastReciever in onResume()):

@Overridepublic void onResume() {    super.onResume();    LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());    IntentFilter filter = new IntentFilter(ACTION_EXAMPLE);    manager.registerReceiver(this.receiver, filter);}

Don't forget to unregister the BroadcastReceiver later on (in onPause()):

@Overridepublic void onPause() {    super.onPause();    LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());    manager.unregisterReceiver(this.receiver);}

And finally you can send local broadcasts like this:

LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());manager.sendBroadcast(intent);


I can extend a broadcast receiver class and register an event

If you mean that you are doing this via LocalBroadcastManager (see Mr. Kapeller's excellent answer for details), Handler will be slightly more efficient, as LocalBroadcastManager uses a Handler. However, the performance difference should not be enough to matter. The same goes for other in-process event bus implementations, such as greenrobot's EventBus and Square's Otto. All should be fast enough that other concerns, such as maintainability, should be paramount.

If you mean that you are doing this via system broadcasts (e.g., sendBroadcast() called on a Context), then Handler, LocalBroadcastManager, or other event bus implementations will be significantly faster, and more secure as well.

All of this assumes that the two services are in the same process.

The fastest solution of all is to combine the two services into one. This is particularly true if they have the same lifespan. There are plenty of cases where having 2+ services in an app is reasonable, but don't create lots of independent little services without a clear reason to do so.


Broadcast receiver calls are heavy operations and there are chances to get ANR's if an event is broadcasted for multiple times. And also the context you get in onReceive() of Broadcast receiver has limited usage until you get application context.

In contrast, handler calls are efficient as they are simple and runs in the different thread and no context is required to start a handler call. communicating between 2 services or activities or 2 threads can be easily handled using handler. Infact all the other ways viz.. Intents and Bound services use handlers internally for message passing.