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

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。

Nik*_*its 3

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

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

您可以在此处查看 Firebase 调度程序

Oreo 中后台服务的其他替代方案。