What is the difference between a background and foreground service?

AndroidAndroid ServiceBackground ServiceForeground Service

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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 http://developer.android.com/reference/android/app/Service.html">here</a>

Solution 2 - Android

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.

Solution 3 - Android

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.

Solution 4 - Android

@Alex User comes to know about the foreground service whenever user gets notified with the notification as per application.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew GuentherView Question on Stackoverflow
Solution 1 - AndroidAsahiView Answer on Stackoverflow
Solution 2 - AndroidDineshKumarView Answer on Stackoverflow
Solution 3 - AndroidN DroidevView Answer on Stackoverflow
Solution 4 - AndroidJaimin PatelView Answer on Stackoverflow