Android Question Do I still need a foreground service? (research for API 34)

Sandman

Expert
Licensed User
Longtime User
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:
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

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?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Sorry, I was trying to piece together information about API 34 from several threads to get a better understanding of the situation.

I should have mentioned that it is a B4XPages project. Unfortunately I don't remember if I converted it to B4XPages before or after I added support for geofences. So I don't remember if I started using a service to solve a pre-B4XPages problem or not. But I do have a distinct memory of a time where geofences just didn't work for me, if I quickly put the app in the background. And I found a working solution in using a foreground service for that. So my conclusion was that the OS happily killed my app quickly, before all geofences could be created.

But are you saying that when I use B4XPages should be able to trust the OS to not pause or kill the app if the user puts it in the background instantly after starting it? Meaning that the app will still have time to create all geofences, with no use of services?
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Your app will not be killed immediately. I'm sure that it will run long enough for the geofences to be created.
Interesting, thanks!

(I'm not sure I dare do this change though, I need to think about it - I need to be real careful about it, they are important for my app. Geofences are hellish that way, they are a real hassle to test, especially if one considers different API versions and phone models.)
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
(I'm not sure I dare do this change though
Me, in these sorts of situations, I make a copy of my project then I make changes to the original (yes I know that B4X creates zipped up backups of projects). Anyway, just make a copy and make the changes, I shouldn't take that long to accomplish.

Btw your app sounds interesting...
 
Upvote 0
Top