Art*_*ans 5 android google-location-services android-8.0-oreo
Android 8(Oreo)引入了一些后台执行和位置限制。有两种方法来请求位置更新-通过用于前台请求的循环程序和通过用于后台请求的未决意图(即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)
Run Code Online (Sandbox Code Playgroud)
我正在考虑对此进行一些电池优化(例如,Android Doze或OS将应用置于待机模式),但是其他应用正在发送通知,因此,它不在Doze中,并且应用处于待机状态也没关系通过PendingIntent发送到BroadcastReceiver。
| 归档时间: |
|
| 查看次数: |
3751 次 |
| 最近记录: |