所有服务的 Android 位置侦听器
我发现了很多创建位置侦听器的示例,您可以在其中提供特定的提供程序,如下所示:
LocationManager lm =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
但是,我不需要精确的位置,但我会采用最好的位置。有没有一种方法可以创建一个在给定时间发挥最佳作用的侦听器,而无需为每个提供者创建侦听器?
例如,如果可用,则使用 GPS,如果不可用,则使用网络等。此外,这是一个小部件,因此我不想检查可用的内容,然后为此创建一个侦听器,因为该小部件将运行很长时间持续时间,并且可能通过启用/禁用提供程序而存在。
感谢您的帮助
I have found plenty of examples of creating a location listener where you supply a particular provider, like so:
LocationManager lm =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
However, I don't need a precise location, but I'll take the best available. Is there a way to create a listener that works for the best available at the given time without creating a listener for each provider?
e.g. Use GPS if it is available, if not, Network, etc. Also, this is a widget, so I don't want to check what is available, then create a listener for that, since the widget will be up for a long duration and may live through enabling/disabling providers.
Thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否考虑过使用
PASSIVE_PROVIDER
?与getLastKnownLocation() 结合使用
可能会让您大部分到达您想要的位置...您最初会调用getLastKnownLocation()
来建立位置(如果设备已经知道该位置),然后PASSIVE_PROVIDER
将侦听来自任何位置提供程序的更新(即从设备上的任何其他应用程序触发的位置请求)。仍然可能出现没有其他应用程序请求位置的情况,因此您可能希望使用
GPS_PROVIDER
或NETWORK_PROVIDER
至少触发一次初始位置修复。Have you considered using the
PASSIVE_PROVIDER
? That combined withgetLastKnownLocation()
might get you most of the way to what you want... You would callgetLastKnownLocation()
initially to establish a location if one was already known to the device, then thePASSIVE_PROVIDER
would listen for updates from any location provider (i.e., location requests triggered from any other application on the device).There would still be the possible scenario of no other application requesting location, so you'd likely want to trigger at least a single initial location fix using
GPS_PROVIDER
orNETWORK_PROVIDER
.