Android Question [SOLVED] Any reason to run Service.StartForeground while app is in foreground?

Sandman

Expert
Licensed User
Longtime User
I just need to do a little bit of sanity check here. Let's say I have a service that's run periodically. If the app is in the foreground (and screen is on, etc), would there ever be a reason to use Service.StartForeground?

(I do realize I need to use it if the app is in background, just curious of when app is in foreground.)
 

JordiCP

Expert
Licensed User
Longtime User
(just thinking) You know that the app is in Foreground when the Service is called but can't tell if it will be Paused for whatever reason just after starting this service.
Perhaps it is only an edge case and is not likely to happen, but if the service task can last a bit (connect to server...) and the Activity has been paused, it could be killed
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
So you're saying the correct strategy is to StartForeground it, regardless if the app is in foreground or background - as a way to minimize potential problems that might be caused by end user doing something while service is running?
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
So you're saying the correct strategy is to StartForeground it, regardless if the app is in foreground or background - as a way to minimize potential problems that might be caused by end user doing something while service is running?
To me it seems a bit more robust as it can cover more scenarios, but may be missing other things
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Another one: Android/Google wants the user to be noticed if a service is running (and not doing "bad" things in the background). So the standard behaviour on the newest Android versions is to start it in foreground (except services where you stop it e.g. firebase messaging - it is then started by the OS via intent - which is ok)
 
Upvote 0
Top