请告诉我的应用程序,一旦用户打开/关闭 GPS,我就可以根据 GPS 可用性切换位置提供商。 我的应用程序是一项服务,我在我的服务类的 onCreate() 方法中注册了这两个提供者。
最佳答案
如果调用 LocationManager.requestLocationUpdates(...)
在你的LocationListener
你会重写 onProviderDisabled
和 onProviderEnabled
并且在这两种方法中你会做任何你想做的事情 - 切换你正在使用的提供者 - 在这种情况下。
如果您需要进一步的解释,请发表评论让我知道。
编辑:
So should i remove the listener like this locationManager.removeUpdates(locationListener); in onProviderEnabled() before registering for the listener according to their availabilty?
没有。使用 2 个条件 if
语句,如下所示:
LocationManager locMan = (LocationManager)Context.getSystemService(Context.LOCATION_SERVICE);
if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){
//request updates for gps provider
}
if (locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
//request updates for network provider
}
这样,如果两个都打开,您将同时使用两个,但如果只有一个打开,您将只使用那个。如果两者都关闭,您当然不会使用任何一个。
你可能还想做:
if (!locMan.isProviderEnabled(LocationManager.GPS_PROVIDER) &&
!locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
//do whatever you want for when both are disabled.
}
关于android - 如何知道一旦用户打开/关闭 GPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8711215/