Sometimes your device settings is set to get the location using WiFi Networks not GPS system, so that the location would be opened but your app will return false when checking for the GPS_PROVIDER
.
The proper solution to that is to check for both, GPS & Network:
if you want to check using Settings
:
private boolean checkIfLocationOpened() {
String provider = Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (provider.contains("gps") || provider.contains("network"))
return true;
}
// otherwise return false
return false;
}
and if you want to do it using the LocationManager
:
private boolean checkIfLocationOpened() {
final LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) || manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
return true;
}
// otherwise return false
return false;
}
You can find the complete details at my answer here.