Reference
Problem
- Apps should ask for location permissions only when used.
- If a foreground service uses location, it must be passed to startForeground().
- My app starts startForeground on app start with other permissions.
- The user later grants location permission. The foreground service must startForeground() again with the updated serviceType.
- There is no documentation provided for correctly restarting a foreground service to update foregroundServiceType in the Android documentation.
Workaround ideas
In the service, I might try to check if it’s running, stopForeground(), and stopSelf(), then startForeground. To not have to restart the foreground service needlessly if serviceType hasn’t changed, I must read foregroundServiceType (of my running service).
I have serviceInfo.service for my foreground service, but I can’t seem to read foregroundServiceType to be able to compare.
I would much rather learn of a proper official way.
Tried: A condition around startForeground based on serviceInfo.
Expected: To be able to read foregroundServiceType from a running foreground service.
Result: Unable to read.