简体   繁体   English

如何侦听启用/禁用的位置提供程序?

[英]How to listen for location providers becoming enabled / disabled?

是否可以在启用或禁用位置提供程序时得到通知,而无需实际查找设备的位置(因为这样做可能会消耗大量电能)?

In android there is LocationListener interface which provides a abstract method onProviderEnabled(String provider). 在android中,有LocationListener接口,它提供了onProviderEnabled(String provider)的抽象方法。 So you have create your own listener and implement the onProviderEnabled(String provider) method. 因此,您已经创建了自己的侦听器并实现onProviderEnabled(String provider)方法。 It is very must your listener must be register with location manager. 您的听众必须在位置管理器中注册,这是非常必要的。

If do same as described above then onProviderEnabled(String provider) will called automatically when location provider became enable. 如果执行与上述相同的操作,则在启用位置提供程序后会自动调用onProviderEnabled(String provider)。

Locationmanager.isProviderEnabled() doesn't seem to require actually looking up the location. Locationmanager.isProviderEnabled()似乎不需要实际查找位置。 It won't be too efficient as you'd need to poll it, but it's better than getting the actual location. 它不会很高效,因为您需要对其进行轮询,但是比获取实际位置要好。

Actually, reading a bit more on the topic, it seems you'd be able to use requestLocationUpdates() to do this. 实际上,阅读有关该主题的更多内容,看来您可以使用requestLocationUpdates()来完成此操作。 It will send you a bundle with KEY_PROVIDER_ENABLED when the provider is enabled or disabled. 启用或禁用提供程序时,它将向您发送包含KEY_PROVIDER_ENABLED的捆绑KEY_PROVIDER_ENABLED You can set the minimum time and distance for notification to extremely high values. 您可以将通知的最短时间和距离设置为极高的值。 This way you'll get a location notification once in a while and mostly just enabled/disabled notifications. 这样,您会偶尔收到一次位置通知,并且大多数情况下只是启用/禁用了通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从多个提供商处收听位置更新? - How to listen for location updates from multiple providers? 启用的GPS提供程序列表与禁用的GPS提供程序列表相同 - List of enabled GPS providers same as list of disabled GPS providers Android - 如果启用或禁用GPS,有没有办法收听 - Android - Is there a way to listen if GPS was enabled or disabled 如何在一次测试中启用位置并在第二次测试中禁用位置进行测试? - how to instrumentation test with location enabled in one test and disabled in the second test? 禁用所有位置提供程序后,如何在android中获取位置? - How can you get a location in android when all location providers are disabled? android:当用户启用或禁用“我的位置”设置位置服务时如何引起注意 - android :how to get noticed when the My Location setting location service is enabled or disabled by user 使用融合位置提供程序获取启用/禁用位置服务的信息 - Get information of Location Services enabled/disabled using Fused Location Provider 如何接收已启用/禁用的GPS? - How to receive gps enabled/disabled? IsProviderEnabled始终返回true启用或禁用位置服务 - IsProviderEnabled always return true either Location service is enabled or disabled 如何设置禁用移动数据的接收器? - how to set receiver for mobile data enabled disabled?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM