Thursday, April 12, 2012

GPS is not enabled but isProviderEnabled() is returning true

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





1 comment:

  1. Modify your code---

    LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ Intent gpsOptionsIntent = new Intent( android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(gpsOptionsIntent); } else{ Toast.makeText(this,"GPS IS ENABLED", 5000).show(); }

    ReplyDelete