What is the difference between a background and foreground service?
Asked Answered
T

4

70

I am currently writing my first Android application and I keep running into references to background and foreground services. Since I intend on using a service in my application I was hoping to get a clarification between the two and how they are used.

Taster answered 21/8, 2010 at 20:1 Comment(0)
U
59

Perhaps this will answer your question:

A started service can use the startForeground API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. By default services are background, meaning that if the system needs to kill them to reclaim more memory (such as to display a large page in a web browser), they can be killed without too much harm.

More info can be found here

Ultramicroscope answered 21/8, 2010 at 20:30 Comment(2)
How can you know if your service really started as foreground service? is there any trace in logcat?Colenecoleopteran
Whether the service is in foreground or background depends on two factors: the service state and the way you started it. developer.android.com/reference/android/app/…Anglesey
S
61

Foreground: The process relies on onPause() and onResume()...i.e you play music player and pressing pause and play

Background: The process which runs without user interaction i.e receiving a message, incoming call, receiving mails, or setting alarms. The method used here is onStart() and onStop().

For example, check it on your phone. Create an alarm at 6:30am. When the system clock reaches 6:30am it fires. In order to kill the alarm service, just go to menu-->settings-->application-->Running service-->click stop service. It stops the alarm service even when your system reaches the time it won't fire.

Sander answered 17/2, 2012 at 5:50 Comment(1)
Better to attach a link to the relevant official documentation, like this one: developer.android.com/guide/components/servicesSpaceless
U
59

Perhaps this will answer your question:

A started service can use the startForeground API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. By default services are background, meaning that if the system needs to kill them to reclaim more memory (such as to display a large page in a web browser), they can be killed without too much harm.

More info can be found here

Ultramicroscope answered 21/8, 2010 at 20:30 Comment(2)
How can you know if your service really started as foreground service? is there any trace in logcat?Colenecoleopteran
Whether the service is in foreground or background depends on two factors: the service state and the way you started it. developer.android.com/reference/android/app/…Anglesey
B
5

Foreground Service is used when User is interaction with application and when Service is doing something visible to user. Background Service is used when even user close application (discard from recents) and when Service is doing something not visible to user like downloading data from server, load data from a ContentProvider etc.. And Foreground Service is less likely to be killed by system on low memory.

Bosworth answered 18/5, 2019 at 6:45 Comment(0)
C
-1

In Android all services behave in a way that's conceptually similar to daemons or batch processes, i.e. they will execute long-running tasks without user interaction both when the application is in the foreground and at least some of the time in the background. But note that the docs say they execute on the applications main thread by default, so they're not true daemons but are part of the applications process.

A foreground service will not be terminated if the system is low on memory or if your application is swiped away by the user. Infact, even after the application is swiped away the application process itself will continue to execute so long as the service is running. It will run when the app is in the background or idle, too. However, a notification must be shown to the user to justify this. So it is guaranteed to execute its task (unless the user cancels it via the task manager, which they can do as of Android 13, but this will actually kill your entire application process).

A background service does not require showing the user a notification, but if the system gets low on memory the service may be terminated, and if your application is swiped away the service will be terminated. So the task is not guaranteed to execute. Also note that after a few minutes of the app being in the background, when the app goes idle, the background service will be stopped as well, but can be explicitly resumed when the app returns to the foreground. This works because all services are sticky by default, see onStartCommand() and START_STICKY in the docs for more details.

Nowdays instead of using a background service people often use the WorkManager instead.

Foreground services typically require more user permissions than background services.

Coachandfour answered 13/6 at 4:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.