【问题标题】:Can we configure android Work Manager to run when there is location change based on distance?我们可以配置 android Work Manager 在根据距离发生位置变化时运行吗?
【发布时间】:2019-08-18 06:02:36
【问题描述】:

我需要一个为我的应用程序持续运行的后台服务。我在网上寻找了不同的方式,例如前台服务,它不断显示 android 8 以上版本的通知,工作管理器和作业调度程序。我认为Work Manager在后台运行方面适合我的要求,但不确定是否可以根据位置距离变化触发。

https://developer.android.com/topic/libraries/architecture/workmanager

我看到我们可以像每 15 分钟一样定期更新,但想知道我们是否可以根据位置距离变化本身进行配置。

https://medium.com/@prithvibhola08/location-all-the-time-with-workmanager-8f8b58ae4bbc

例如,如果我为位置请求设置setSmallestDisplacement(10m),则应每10m距离更改一次触发worker manager并将位置发送到服务器。

https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest

【问题讨论】:

  • LocationRequests 已经在后台运行,即使在 Android 8+ 上也是如此。这就是为什么接受PendingIntent
  • 那么是否可以在不运行服务的情况下获取位置更新?你能给我提供好的例子或文档来帮助我吗?@ianhanniballake
  • 不要指望setSmallestDisplacement 对 10m 的变化敏感。要获得可靠的结果,请使用 100m+。还要考虑这一点:developers.google.com/awareness/android-api/fence-api-overview 最后一件事 - 您不需要服务,它不是 PC/服务器。如今,服务在 Android 中有点用处。任何后台任务机制(jobscheduler、workmanager 等)都会满足您的所有需求。
  • 希望对您有所帮助:stackoverflow.com/a/56593800/1318946

标签: android google-maps android-location android-workmanager foreground-service


【解决方案1】:

对于我的应用,我会让定位服务无计划地运行。但是我会在我的位置管理器中指定最小距离来调用这样的更新。

private void onStartListening() {  
   //init location manager.
   LocationManager mLm = (LocationManager) getSystemService(LOCATION_SERVICE);

   //binding listener to manager with network provider.    
   mLm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 10, mNetworkListener);
}

requestLocationUpdates() 的第三个参数是位置更新之间的最小距离(以米为单位)。

另外,第二个是位置更新之间的最小时间间隔(以毫秒为单位)。如果您还想为您的位置更新指定时间标准。

【讨论】:

  • 我希望它甚至在后台运行,如果您没有运行服务或工作管理器,我认为位置管理器不会在后台连续运行。
  • 如果你把LocationManager放在服务中,只为requestLocationUpdates()配置最小距离会怎样
  • 我要避免的是使用服务。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多