简体   繁体   中英

How to listen for location providers becoming enabled / disabled?

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

In android there is LocationListener interface which provides a abstract method onProviderEnabled(String provider). So you have create your own listener and implement the onProviderEnabled(String provider) method. 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.

Locationmanager.isProviderEnabled() doesn't seem to require actually looking up the location. 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. It will send you a bundle with KEY_PROVIDER_ENABLED when the provider is enabled or disabled. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM