【问题标题】:Background location updates on Android Oreo stop after 2 hoursAndroid Oreo 上的后台位置更新在 2 小时后停止
【发布时间】: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


【解决方案1】:

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

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

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

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

【讨论】:

  • 正如您在回答中所建议的那样,应该必须实施 firebase 作业调度程序,因此它是一种推动电话再次启动服务的方式。我对吗 ? android端有没有其他方法可以在不从服务器推送的情况下触发它?
  • 不敢相信这可能是预期的解决方案 - 如果它不起作用,提供后台位置侦听器有什么意义......
猜你喜欢
  • 1970-01-01
  • 2017-12-16
  • 2019-11-21
  • 1970-01-01
  • 1970-01-01
  • 2019-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多