[Solved] difference between services and broadcast receivers
im trying to understand what the difference between a service and a broadcast receiver is,
as i see it they can do the same thing.
For example i have an application : App1
that provide a service called ToastHelloWorld which just creates a Toast and stopSelf().
I expose it to other applications using an intent filter with the action name: “com.test.HelloToast”
Now i have another application : App2
i want to implicit use a service with the action “com.test.HelloToast” so i call
and it works.
Why would i use broadcast receivers when i can do everything with services and dont have the restriction of a 5sec execution limit?.
I know most “system events” is exposed via broadcasts’ but couldnt they just aswell be published as Service Intents ?
Broadcast intents are usually delivered to all BroadcastReceivers registered for that intent. (There is an exception with ordered intents where a receiver can abort the delivery for lower priority receivers) Intents that start or bind services are only sent to one matching Service instance.
Some broadcast intents are sticky. That means they might have been sent in the past and will be delivered when your app registers the receiver.
The biggest difference is that a BroadcastReceiver runs on the main UI thread by default (you can specify a different thread), and thus it is limited to the 10 second execution rule. Services, on the other hand, can run indefinitely until the OS decides to kill them.
Service is used when you want to do something in background, any long running process can be done using Service in Background. For example, you want to play music when your application gets close. In that case service will be running in background with music.
BroadcastReceiver is used when you want to fire some stuff or code during some event. For example, event can be on Boot of Device. If you want to perform something when device Boots, date and time changed etc…