Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between services and broadcast receivers

Tags:

android

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 startService(new Intent("com.test.HelloToast"));

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 ?

like image 280
Mads Lee Jensen Avatar asked Jul 17 '10 18:07

Mads Lee Jensen


1 Answers

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.

like image 96
Dirk Jäckel Avatar answered Sep 21 '22 14:09

Dirk Jäckel