Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a background and foreground service?

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.

like image 994
Andrew Guenther Avatar asked Aug 21 '10 20:08

Andrew Guenther


People also ask

What is the difference between foreground and background service?

For Android Developers, a Service is a component that runs on the background to perform long-running tasks. A Background Service is a service that runs only when the app is running so it'll get terminated when the app is terminated. A Foreground Service is a service that stays alive even when the app is terminated.

What does it mean when an app is using foreground service?

Foreground services perform operations that are noticeable to the user. Foreground services show a status bar notification, so that users are actively aware that your app is performing a task in the foreground and is consuming system resources.

What is the difference between foreground and background data?

"Foreground" refers to the data used when you're actively using the app, while "Background" reflects the data used when the app is running in the background.

What are background services in Android?

A background service performs an operation that isn't directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.


2 Answers

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

like image 59
Asahi Avatar answered Sep 22 '22 20:09

Asahi


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.

like image 39
DineshKumar Avatar answered Sep 22 '22 20:09

DineshKumar