I\'ve got this code
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
boolean isGPS = lm.isProviderEnabled (LocationManager.GPS_PROVI
some time location manager returns the wrong result.below code works for me.
public static boolean isGpsEnabled(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
String providers = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (TextUtils.isEmpty(providers)) {
return false;
}
return providers.contains(LocationManager.GPS_PROVIDER);
} else {
final int locationMode;
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
switch (locationMode) {
case Settings.Secure.LOCATION_MODE_HIGH_ACCURACY:
case Settings.Secure.LOCATION_MODE_SENSORS_ONLY:
return true;
case Settings.Secure.LOCATION_MODE_BATTERY_SAVING:
case Settings.Secure.LOCATION_MODE_OFF:
default:
return false;
}
}
}