I'm trying to update my app for the upcoming API 34 requirement, and as best as I can tell the only real thing to care about is if one uses foreground services.
My app uses foreground services, so obviously I need to adjust some things for API 34. But then I saw that Erel posted this:
Which made me wonder if I really needed a foreground service at all. I'd love input from the forum members on whether I actually need it or not.
About my app
My app uses geofences. They are stored locally in a small database, and on app launch the fences are registered with the OS. (Once the geofences trigger things happen that are not relevant to this thread. The permissions for geofences are also out of scope for this thread.)
Actual things happening
So why do I do this in a service at all? It can take some time to register a geofence, it's up to how the OS prioritize things. I think I remember it can take several seconds for a single geofence on some of my test devices. The typical user behaviour for my app is to start it, do something quick and move the app to the background. While in the background the app can quickly be killed by the OS if we're unlucky - which means that not all geofences will have had time to be created. That would be considered a very bad thing.
So I have a foreground service that only lives for max a minute perhaps, while registering geofences.
Given that I'm not starting that service from the background (it starts when the user starts the app), could I simply stop making it a foreground service and still trust the geofences to be created? Or is the whole "protect from OS killing app if the user throws app into background" still a valid reason to keep it a foreground service?
My app uses foreground services, so obviously I need to adjust some things for API 34. But then I saw that Erel posted this:
Note that the StartForegroundService code is not required unless you are trying to start the service from the background (from a Receiver most probably). In other cases you can simply call the StartService keyword.
Which made me wonder if I really needed a foreground service at all. I'd love input from the forum members on whether I actually need it or not.
About my app
My app uses geofences. They are stored locally in a small database, and on app launch the fences are registered with the OS. (Once the geofences trigger things happen that are not relevant to this thread. The permissions for geofences are also out of scope for this thread.)
Actual things happening
- User starts app
- A foreground service is instantly started with a notification: "Creating geofences for you"
- It waits for 5 seconds (why? I don't remember anymore, I didn't comment the code well enough.)
- It loops through list of geofences, registering each one
- Once done, it stops itself, which also removes the notification
- A foreground service is instantly started with a notification: "Creating geofences for you"
So why do I do this in a service at all? It can take some time to register a geofence, it's up to how the OS prioritize things. I think I remember it can take several seconds for a single geofence on some of my test devices. The typical user behaviour for my app is to start it, do something quick and move the app to the background. While in the background the app can quickly be killed by the OS if we're unlucky - which means that not all geofences will have had time to be created. That would be considered a very bad thing.
So I have a foreground service that only lives for max a minute perhaps, while registering geofences.
Given that I'm not starting that service from the background (it starts when the user starts the app), could I simply stop making it a foreground service and still trust the geofences to be created? Or is the whole "protect from OS killing app if the user throws app into background" still a valid reason to keep it a foreground service?