android icon indicating copy to clipboard operation
android copied to clipboard

Handle foreground service restrictions for SensorWorker

Open jpelgrom opened this issue 2 years ago • 0 comments

Summary

Resolves #2164, part 2: SensorWorker.

The SensorWorker currently always starts as a foreground service, which may not be allowed on Android 12 and newer. Because it can also work in the background catch a ForegroundServiceStartNotAllowedException if it is thrown, to update the sensors as much as possible instead of failing/no update.

We could build a complicated check to determine if it is likely that the app will be allowed to start a foreground service, but there don't appear to be any negative consequences for only catching the exception.

While running this build over the last ~8 hours I didn't notice a substantial difference in update frequency when compared to the (also battery optimized) stable version of the app. In one update it didn't allow the stable version to start a foreground service, which this PR will resolve.

Screenshots

n/a

Link to pull request in Documentation repository

I don't think we need additional documentation, allowing unrestricted background access / disabling battery optimization is already recommended in general for power users.

Any other notes

Part 1 is #2790 - no need to merge them in the same order though.

jpelgrom avatar Aug 12 '22 13:08 jpelgrom