Friday, June 29, 2018

What's the difference between a service and a broadcast receiver?


I keep hearing about services and broadcast receivers. What's the difference between them, and how do they affect the operation of my Android phone from my point of view?




Answer



Android applications have three kinds of components. In general, users don't need to know about them: they're a way for app authors to program particular behaviours into their apps. But if you're watching your apps' behaviour closely such as with a task manager, or if you are automating things with an app like Tasker, it's helpful to understand how they fit together within the system.


An activity is the most familiar type of component: it's a window you can see: either full-screen or dialog-sized. An activity only runs while it's displayed on the screen. Once you leave the activity, Android will keep that app in memory ready to be started again, but the activity won't run, meaning it won't use battery or network. An app starts an activity using an intent. The intent can specify explicitly which activity to start, or it can specify an action to perform (such as opening a particular file). If more than one activity can "handle" the intent, you see the dialog asking you to choose one.


A service is another application component. Once another component (maybe an activity, or another service) has started a service, it runs in the background until it stops itself. This means that a service could be keeping your phone awake (using a wake lock), running down the battery, or using lots of network data, without anything showing on the screen.


Apps can use services to do long-running processes in the background, such as downloading files from a server, or checking for email, or checking your location. Although services don't show up or interact with you directly, they still show up in the "Running apps" list. From the Settings app, choose Apps or Applications manager, and then Running. You can stop a service that way, but it's not usually necessary. Because services aren't directly visible, Android considers them less important than activities, so they'll be the first to be killed when your phone needs more memory.


Broadcast receivers are the third kind of component. Like services, they only exist in the background and don't interact with you directly. But unlike services, they can't stay running or perform long tasks: they exist to respond to events. And unlike activities and services, more than one broadcast receiver can be started in one go.


A component broadcasts an intent, possibly to one app, but more often without specifying a particular app. In this case, the intent usually represents an event that's happened, such as the battery running low. The system finds all the broadcast receivers that have registered an interest (using an intent filter), and runs each in turn. Each broadcast receiver can react straight away, for example by creating a notification, or it can start a service or an activity to take further action. As soon as the broadcast receiver has handled the event, it is stopped and will not run again until another similar event is broadcast.


An example of all three components working together is when you download an app from Google Play. First, the Google Play activity provides the visual interface for you to choose the content to download. The list of apps might be one activity; clicking the "install" button starts another activity to show the confirmation or payment dialog. When you confirm, the dialog activity starts a service. The service will continue to download the content even when the activity has finished and is no longer running.


But perhaps the phone loses Internet connection while the new app is downloading. If this happens, Google Play's download service will register a broadcast receiver, with an intent filter to say that it's interested in network connectivity changes, and then the service will stop itself. When the phone connects to the Internet, the system broadcasts an event. Android will start the broadcast receiver the Google Play service registered, along with any other broadcast receiver waiting for that event. In this case, the broadcast receiver will start the download service again. The service will create notifications as the download proceeds, and when it finishes, it will send its own broadcast to inform other apps that the new package has been installed, and will then stop itself. In turn, this will start broadcast receivers from other apps, to update the list of apps in the launcher, etc.


In summary:-




  • An activity represents a window on the screen; a service performs a possibly long-running background task; a broadcast receiver runs for a short time, to handle an event.

  • All three are started using intents, but using a different mechanism. The launching app has to state explicitly which type of component to start (by calling a different method in each case).

  • When an app starts an activity using an intent, it only starts one activity (possibly showing the "Complete action using..." dialog to let you choose which), and the same goes for services, but broadcasting an intent may start several broadcast receivers, possibly from different apps.

  • As a user, you interact with activities directly; you don't interact with services, but they can slow down the phone and consume resources; you don't interact with broadcast receivers, and because they're short-lived you don't need to worry about managing them.


No comments:

Post a Comment

samsung galaxy s 2 - Cannot restore Kies backup after firmware upgrade

I backed up my Samsung Galaxy S2 on Kies before updating to Ice Cream Sandwich. After the upgrade I tried to restore, but the restore fails ...