【发布时间】:2018-08-01 21:01:00
【问题描述】:
Android 8 (Oreo) 引入了一些Background Execution and Location limits。有两种方法可以请求位置更新——通过looper 用于前台请求,通过pending intent(即BroadcastReceiver)用于后台请求。
如果您的应用在后台运行,定位系统服务每小时只会为您的应用计算几次新位置。
问题: 当应用程序进入后台模式时,我会在大约 2 小时内收到位置更新,然后直到我再次真正打开应用程序后才收到位置更新。我的 LocationRequest 设置没有指定任何到期时间,因此,我希望无限期地接收位置更新。
val locationRequest = {
val mLocationRequest = LocationRequest()
mLocationRequest.interval = 4.minutes()
mLocationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
mLocationRequest.maxWaitTime = 10.minutes()
mLocationRequest.fastestInterval = 30.seconds()
mLocationRequest
}()
val intent = Intent(this, BackgroundLocationUpdatesReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
LocationServices.getFusedLocationProviderClient(mContext).requestLocationUpdates(locationRequest, pendingIntent)
我正在考虑一些电池优化来解决这个问题(即 Android 打盹或操作系统将应用程序置于待机模式)但其他应用程序正在发送通知,因此,它不在打盹和应用处于待机状态并不重要,因为它是通过 PendingIntent 发送到 BroadcastReceiver。
【问题讨论】:
-
这个新功能太糟糕了,连谷歌地图的位置分享功能都失效了,我再也无法获取朋友的当前位置了
标签: android android-8.0-oreo google-location-services