前景服务未在Android O中以打ze模式接收位置更新 [英] Foreground service not receiving location updates in Doze mode in Android O

查看:97
本文介绍了前景服务未在Android O中以打ze模式接收位置更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个前台服务,需要进行一次位置更新.

它由特定的广播开始,并且在开始时仅在我们的后端发出请求.当它收到另一个广播时,它一次请求位置更新,再执行另一个后端请求,然后自行停止.

为澄清起见,服务本身会进行位置更新和后端请求.在这方面,它与主应用程序无关.

当手机处于唤醒状态或处于打ze模式时,此功能在Lollipop和棉花糖上均能很好地工作.但是由于某种原因,在Android O上,在打Do模式下该服务永远不会获得位置更新.在这两种情况下,网络都可以很好地处理其第一个请求.另外,如果在位置请求运行时唤醒手机,它将获取更新.如果将其放在维护窗口中也一样.

Android O确实在背景位置上引入了一些限制.但正如页面所提到的,应保留前台应用程序的行为(应用程序可见或正在运行的前台服务),并且应与以前的Android版本相似.

这是开发人员预览中的问题还是我错过的限制?

解决方案

看看LocationUpdatesForegroundService示例: Java

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?

解决方案

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

这篇关于前景服务未在Android O中以打ze模式接收位置更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆