Kasas
Kasas

Reputation: 1216

IsProviderEnabled for GPS is not working

we have implemented a geobased app for Android, so we need to ensure GPS is enabled all the time. The problem is that

manager.isProviderEnabled( LocationManager.GPS_PROVIDER )

is always returning false even if the GPS Provider is enabled, so our app is always showing the alert to change the GPS status or it is not working.

Do you know what is happening?

We are testing it with a Samsung Galaxy S and a HTC Wildfire device... Thanks in advance.

Upvotes: 5

Views: 4138

Answers (3)

Muhammed Refaat
Muhammed Refaat

Reputation: 9103

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.

Upvotes: 0

user584187
user584187

Reputation: 1

You need to check first whether GPS really present on your phone. If your phone is cheap phone most probably it use network location as location provider.

You may try this:

private boolean isGPSEnabled() {
Context context = Session.getInstance().getCurrentPresenter().getViewContext();

LocationManager locationMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

boolean GPS_Sts = locationMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER)|| locationMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);

return GPS_Sts;
}

Upvotes: 0

cdelolmo
cdelolmo

Reputation: 66

You can get GPS status directly from system:

LocationManager myLocationManager = (LocationManager)  getSystemService(Context.LOCATION_SERVICE);

private boolean getGPSStatus()
{
   String allowedLocationProviders =
   Settings.System.getString(getContentResolver(),
   Settings.System.LOCATION_PROVIDERS_ALLOWED);

   if (allowedLocationProviders == null) {
      allowedLocationProviders = "";
   }

   return allowedLocationProviders.contains(LocationManager.GPS_PROVIDER);
} 

Upvotes: 5

Related Questions