Search code examples
androidserviceforeground

What is a Foreground Service in Android, in essence. LocationListener onLocationChanged


What really is a foreground service in Android? I have an app with Activities, and Services and uses-permission android.permission.FOREGROUND_SERVICE in the manifest. It needs to get LocationListener onLocationChanged callbacks even when the Activity in not in the foreground. Currently it only gets onLocationChanged callbacks when the Activity is in the foreground.

It does respond to TCPIP requests sent its TCP listen port all the time, even when the Activity is not open, so I think it is alive all the time.

I think of these things from a Unix/Linux sense, that the App/Service/Activities are all one Linux process and that the FOREGROUND_SERVICE in the manifest refers to the whole process. I think of threads as all being more of less equivalent.

In Android, the 'main' thread of an activity is special (for the UI control). That thread contains a 'Looper? Which is a message queue and presumably the main UI thread perpetually dequeues messages and processes them and calls client UI callbacks.

Is being a 'foreground service' something associated with a particular thread, like the one that calls the 'onCreate' callback for the 'Service' (from which I call the startForeground method?) Is the foreground service associated with thread that calls-back the onStartCommand callback? I would think that the whole process and all its threads are the 'foreground service'.

So, if the whole thing is not a foreground service, what thread or context or part of the thing is a foreground service (and thus worthy of getting onLocationChange callbacks even if the Activity is not being shown)?

(Note: I've been confused by the terms foreground and background. I thought that 'services' were sort of 'background' things, that provide services, but have no visible UI. The term 'foreground service' in Android means a service that: performs a task that is noticeable by the user, even when they're not directly interacting with the user. So I think 'foreground service' is a bit of a misnomer.)


Solution

  • Do you have background location permission? On modern Android, getting location in the background requires special permission. See https://developer.android.com/about/versions/oreo/background-location-limits and ACCESS_BACKGROUND_LOCATION permission

    But to answer your question- a Service in Android is just an instance of Context that is not associated with a UI. It has no particular thread associated with it (the lifecycle methods all run on the main thread, like all lifecycle methods do). It's part of the main process. There's nothing special about it, it's just an Android Context with it's own lifecycle.

    A Foreground Service is an Android Service that's marked as being in the foreground by the OS. This is not a publicly settable field, it's something stored by the OS in its memory. Foreground services have additional privlidges (there are things that background services aren't allowed to do, like record audio) and are less likely to be ended by the system for resources. But nothing changes about it's structure or threads.