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
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With