I know that onReceive() of the Broadcast receiver and handleMessage() of Handler run on the same UI thread. Suppose I want to communicate between two services, within the same a
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.