简体   繁体   中英

Recording GPS location in the background doesn't work when battery optimizations are enabled on Android

We develop an application which is used for outdoor activities (hiking, bicycle, mountain bike, ...) and one of the main features of the application is to record the track of the user thanks to the GPS of the smartphone / tablet in the background in order to save it on the user account.

In order to do that, we start a foreground service with wake locks and a persistent notification during the recording.

This feature is optimized to use the battery as few as possible.

However, when battery optimizations are enabled on the device, the record of the track doesn't work properly in the background, which leads to straight lines in the track, which becomes unusable.

We have managed to detect that battery optimizations are enabled via isIgnoringBatteryOptimizations() method of PowerManager and redirecting the user to the appropriate procedure to disable battery optimizations manually.

However, it is not very user-friendly.

Our application uses the Android standard location provider and not the Fused Location provider in order to allow using the GPS even in airplane mode. It doesn't request the ACCESS_BACKGROUND_LOCATION permission because it is not recommended and requires a manual review by Google.

We have some questions:

  1. Is it normal for a location based app which records a track in the background not to work properly when battery optimizations are enabled? Or is there something else to take into account / enable in our code / configuration?

  2. Would the problem occur less often if we use

  • the Fused Location Provider
  • activity recognition feature (with ACTIVITY_RECOGNITION permission)
  1. In our use case, is it really forbidden and subject to ban of the application on the Play Store to present to the user the popup to request to ignore battery optimization (with permission REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)?

If it is forbidden, is it normal that some competitor apps which have the same feature display this popup and are still published on the Play Store?

Is it allowed to present this popup if we enable a live-tracking feature in the application to allow the location of the user to be followed by other people for safety reasons (in that case, the application may be allowed for exception "Safety app - Apps that keep their users and their families safe." listed here: https://developer.android.com/training/monitoring-device-state/doze-standby.html#exemption-cases )

Thanks in advance for your help.

Best regards.

As the FusedLocation provider is the recommended location provided by Google, I won't use anything else. then, make sure to use the FusedLocation with the recommended guidelines https://developers.google.com/location-context/fused-location-provider .

Also, make sure to test it under the latest target SDK as the latest is more restrictive.

Yet, I found the battery restriction annoying, missing some basic information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM