is startService() then calling startForeground in service same as startForegroundService call?

61 Views Asked by At

Context.startForegroungService indicates that we want to start foreground service. But we can start service by Context.startService and turn into foreground service by calling startForeground in the service.

Is there any behaviour difference between those? What will we lose if we don't call startForegroundService but convert a normal started service to foreground?

1

There are 1 best solutions below

1
LazyProgrammer On

By calling startService() followed by startForeground() within a service, you are manually starting a service and then promoting it to a foreground service. This approach is typically used for compatibility with older Android versions.

By the way, startForegroundService() was introduced in Android Oreo (API level 26) to start a foreground service directly. It combines both starting the service and making it a foreground service in a single call, simplifying the process. Obviously it's for Oreo and above to ensure compatibility. I would suggest you to read this doc Foreground Service