The difference between services and broadcast receivers

im trying to understand what is the difference between a service and a broadcast receiver, as i see they can do the same.

For example, I have an application: App1 which provide a service called ToastHelloWorld, which simply creates Toast and stopSelf (). I host it in other applications using an intent filter with the name of the action: "com.test.HelloToast"

Now I have another application: App2 I want me to imply using the service with the action "com.test.HelloToast", so I call startService (new Intent ("com.test.HelloToast"));

and it works.

Why should I use broadcast receivers when I can do everything with services and have no limit on the execution limit of 5 seconds?

I know that most “system events” are published through broadcasts, but can't they be published as official intentions?

+5
android
Jul 17 '10 at 18:36
source share
3 answers

Intent broadcasts are usually delivered to all BroadcastReceivers registered for this intent. (There is an intentional exception where the recipient may interrupt delivery for receivers with a lower priority). Devices that start or link services are sent to only one corresponding service instance.

Some translational intentions are sticky. This means that they may have been sent in the past and will be delivered when your application registers the receiver.

+4
Jul 17 '10 at 21:57
source share

A service is used when you want to do something in the background, any lengthy process can be performed using the service in the background. For example, you want to play music when your application approaches. In this case, the service will work in the background with music.

BroadcastReceiver is used when you want to run certain things or code during an event. For example, the event could be on Boot of Device. If you want to do something when the device boots, the date and time have changed, etc.

+3
Aug 27 '12 at 5:25
source share

The biggest difference is that BroadcastReceiver runs on the main user interface thread by default (you can specify a different thread) and, thus, it is limited to a 10-second execution rule. On the other hand, services can run indefinitely until the OS decides to kill them.

+3
Feb 27 '15 at 21:01
source share



All Articles