问题
I have a foreground service that needs to get a single location update.
It is started by a specific broadcast, and upon starting it simply does a request on our backend. When it receives another broadcast, it requests a location update (once), does another backend request and then stops itself.
Edit: to clarify, the service itself does the location updates and backend requests. It is independent from the main app in that regard.
This works well on Lollipop and Marshmallow, both when the phone is awake or in Doze mode. But for some reason, on Android O the service never gets location updates when in Doze mode. Networking works fine as its first request goes through in both situations. Also, if I wake up the phone while the location request it running, it will get the update. Same if I put it in maintenance window.
Android O does introduce some limitations on background location but as the page mentions, the foreground app behavior (app visible or foreground service running) should be preserved and be similar to previous versions of Android.
Is this an issue in the developer preview or a restriction that I missed?
回答1:
Take a look on LocationUpdatesForegroundService Sample: Code Samples for Android O, maybe you do something wrong.
LocationUpdatesForegroundService Sample - Shows how to use a foreground service to get location updates when the app activities are not visible. For apps running on Android O (even apps that target Android 7.0 (API level 24) or lower), background updates are limited to only a few times per hour. Using a foreground service is a way to receive more frequent updates.
Get it on GitHub: Java
来源:https://stackoverflow.com/questions/44811812/foreground-service-not-receiving-location-updates-in-doze-mode-in-android-o