安卓8 (Oreo)推出了一些背景执行和位置限制。有两种方法来请求位置更新--通过前台请求的活套和对后台请求的待决意图 (即BroadcastReceiver)。
如果您的应用程序在后台运行,定位系统服务每小时只为您的应用程序计算几次新的位置。
The problem:当应用程序进入后台模式时,我会收到大约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)
我正在考虑对电池进行一些优化,以便对此负责(即。但其他应用程序也在发送通知,因此,它不在Doze中,而处于待机状态的应用程序并不重要,因为它是通过PendingIntent发送给BroadcastReceiver的。
https://stackoverflow.com/questions/48920167
复制相似问题