Android Oreo上的后台位置更新在2小时后停止

时间:2018-02-22 05:21:03

标签: android android-8.0-oreo google-location-services

Android 8(奥利奥)推出了一些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 Doze或OS将应用程序置于待机模式)但是其他应用程序正在发送通知,因此,它不在Doze和应用程序处于待机状态并不重要,因为它通过PendingIntent发送到BroadcastReceiver。

1 个答案:

答案 0 :(得分:2)

可能的解决方案是Firebase Job Dispatcher。 您可以安排在每2小时或1小时后启动服务的作业。

启动后台服务的逻辑可能会有所不同。我假设这是因为我看到whatsapp在我的设备上的某个时间间隔内以后台消息运行,该设备正在进行O更新。

您可以查看Firebase调度程序here.

Other alternatives 用于奥利奥的后台服务。