Switching between gps and network provider according to the availability

巧了我就是萌 提交于 2019-11-30 15:34:58

Got a solution for this:

 public void onProviderDisabled(String provider) {
   locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
   if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,  locationListener);
   } else {
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
   } 
}

@Override
public void onProviderEnabled(String provider) {
  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);   
  if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  } else {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  }
}

what i missed is locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); in onProviderEnabled() and onProviderDisabled().

Thanks

Looks like you're only requesting location updates in onCreate() method. Once providers change you have to request updates from the new provider, no?

Here's a post on Android blog about locations, it covers monitoring provider changes too:

http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!