使用 fusedLocation API 定期发送位置更新

periodically send location updates using fusedLocation API

Android 编程对我来说是全新的东西,我一直在玩 android 的位置,我有同样的问题,我将定期发送位置更新到服务器在后台,我正在使用 AlarmManager/LocationManager 方法,设置一个具有定义间隔的 alarmManager,然后当触发 alarmReceiver 时,它将获取设备的当前位置(使用 locationManager)并通过其 onReceive 方法将其发送到服务器。我发现这个 FusedLocation 是一个很好的替代品,因为 LocationManager 给我额外的工作来获得最好的位置提供商。是否可以在没有 alarmManager 的情况下使用 fusedLocation api 在后台定期发送位置更新?如果是这样,我该怎么做?提前致谢!

根据 LocationRequest documentation:

In between these two extremes is a very common use-case, where applications definitely want to receive updates at a specified interval, and can receive them faster when available, but still want a low power impact. These applications should consider PRIORITY_BALANCED_POWER_ACCURACY combined with a faster setFastestInterval(long) (such as 1 minute) and a slower setInterval(long) (such as 60 minutes). They will only be assigned power blame for the interval set by setInterval(long), but can still receive locations triggered by other applications at a rate up to setFastestInterval(long). This style of request is appropriate for many location aware applications, including background usage. Do be careful to also throttle setFastestInterval(long) if you perform heavy-weight work after receiving an update - such as using the network.

这使您可以保证根据您使用 setInterval(long), giving you the equivalent behavior to a periodic alarm, but if other apps request location information, you may get location information as often as your setFastestInterval(long) 设置的时间间隔获得位置更新 - 如果您只想以设定的时间间隔更新位置,您可以将最快的时间间隔设置为您的时间间隔。