Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - what is a foreground service? (vs regular service)

Right now I have some class myService extends Service() being called by a startService intent from another context. The service runs some code in onStartCommand and then is destroyed/killed.

What exactly is a 'foreground' service and how is it created? Does it last longer than other services?

If so that would be ideal because I'm tinkering around with a music player in my service that I would want to play in the background and have control of. The issue right now is that my service gets created and destroyed multiple times, so I end up starting too many instances of the music player and lose control of the original one.

Thanks.

like image 423
JDS Avatar asked Jun 18 '11 13:06

JDS


People also ask

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

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. Devices that run Android 12 (API level 31) or higher provide a streamlined experience for short-running foreground services.

What is difference between background and foreground?

The foreground contains the applications the user is working on, and the background contains the applications that are behind the scenes, such as certain operating system functions, printing a document or accessing the network.

What is difference between service and activity in Android?

Services are a unique component in Android that allows an application to run in the background to execute long-running operation activities, on the other hand, an activity, like a window or a frame in Java, represents a single screen with a user interface.


1 Answers

A foreground service is one that, for example, plays music -- something the user would immediately notice if it were killed. Android will favor non-foreground services for killing if system memory gets low, but it can still kill a foreground service if needed.

like image 59
mah Avatar answered Sep 27 '22 19:09

mah