Reputation: 187
I can check if GPS is on or not using isProviderEnabled(). If it is not on, I am launching intent so that user can enable GPS. At the end I am again checking if GPS is enabled by user or not. If user does not enable GPS and come out, still isProviderEnabled() is returning NULL. What could be the issue ? Please guide me.
String provider = LocationManager.GPS_PROVIDER;
// Check if GPS is enabled
boolean enabled = myLocationManager.isProviderEnabled(provider);
if (!enabled) {
// GPS not enabled
Log.d("", "Provider " + provider + " is not enabled");
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
// Consider the case when user does not enable GPS and come out.
} else {
Log.d("", "Provider is enabled");
}
// Re-check if user has enabled or not. (Note: case: user has not enabled GPS)
enabled = myLocationManager.isProviderEnabled(provider);
if(!enabled)
{
Log.d("","provider not enabled");
}
else
{
// Control is coming here though user has not enabled GPS in settings
Log.d("","GPS is enabled");
}
Thanks, Biplab
Upvotes: 5
Views: 7079
Reputation: 8138
Had the same issue where isProviderEnabled()
would return true on devices < lollipop
Based on user370305's answer I updated my method as follows:
private boolean isLocationEnabled() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !provider.equals("");
} else {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
return gps || network;
}
}
Upvotes: 0
Reputation: 560
I have had this issue on an actual physical device.
I did a few tests using mock locations on my physical Android device, and then switched back to real locations using the GPS (the code was clean of all mock locations). A this point, whether the GPS was stopped or not, the application would always return "true" (GPS activated) and for some reason wouldn't register real locations any more.
In this case, rebooting the physical device solved the issue.
Upvotes: 8
Reputation: 109237
Check GPS enable using this code and let me know what happen,
private void CheckEnableGPS(){
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.equals("")){
//GPS Enabled
Toast.makeText(AndroidEnableGPS.this, "GPS Enabled: " + provider,
Toast.LENGTH_LONG).show();
}else{
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivity(intent);
}
}
Upvotes: 8