Difference between Service and Broadcast receivers in android
Asked Answered
C

3

36

I want to know the difference between services and broadcast receivers, can anyone point out an example that can be observed on android mobile devices. Thanks

Chokefull answered 17/1, 2012 at 5:10 Comment(0)
L
40

Service: If you want to do something in background , this will be running always in background even if the application closed. You can create this in separate process and also you can give your service to other app if you want. Downloading any content or Music is good example

Broadcast Reciever: Usually system will send some info which can be recieved by your app if you would wish to ,by registering. And you can do something what you want when that thing happens by using onReceive method. Example is the system will send BroadcastReceiver when new sms arrives or Booting done

Here is good article : Service and BroadcastReceiver

Lankester answered 17/1, 2012 at 5:32 Comment(0)
S
27

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.

Example of Service

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...

Example of BroadcastReceiver

Stolen answered 17/1, 2012 at 5:13 Comment(1)
The difference here is of course obvious but there's 1 usage making the 2 becoming similar in the help of receiving some event. I found that usage of Service when starting to learn about Push Notification using GCM. On the client side they have 2 services GcmListenerService and InstanceIdListenerService - of course we need to use services here so that they can run in background, but in this case they behave much like what a BroadcastReceiver does, there are callbacks similar to OnReceive and they are fired per some broadcast Intent (specified via IntentFilter).Doll
G
1

I think of it possibly a different way. A Service receives intents that were sent specifically to your application, just like an Activity. A Broadcast Receiver receives intents that were broadcast system-wide to all apps installed on the device.

(The reason I say a Service is a bit like an Activity is that: You wouldn't broadcast a message saying "start Activity MyActivity" across all apps installed on the device. It is only for your specific app.)

Of course, as others mentioned, a Service can continue running in the background, whereas a Broadcast Receiver should finish quickly (e.g. if it is running for more than 5 seconds it may be killed by the OS). The Broadcast Receiver can still run in the background (when app is closed) under certain circumstances. For this, it's worth mentioning that there are actually two types of Broadcast Receivers - Manifest-declared, and Context-registered. They have different lifespans and restrictions - the former can receive broadcasts in the background with certain restrictions, while the latter cannot receive broadcasts in the background (app must be running and active) but has no restrictions on the types of intents that can be received.

Both services and broadcast receivers must be specifically invoked (via an intent), but for services this is usually a specific call (e.g. when your app is started or when the user clicks some button) whereas for broadcast receivers they don't need to be explicitly started as they will start anyway when a relevant broadcast is made.

Here's how I would think of it:

Type Displays UI? Can continue running for a long time when app is closed? Can receive intents when app is closed? Intents must specifically target your app? Restricted list of intents that can be specified?
Activity Yes No Yes Yes No
Service No Yes Yes Yes No
Manifest-declared Broadcast Receiver No No Yes No Yes1
Context-registered Broadcast Receiver No No No No No

1: Only if you target Android 8.0 or above. The restrictions are not applied if the intent specifically targets your app. The restricted list of intents can be found here.

Gmur answered 16/5, 2021 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.