5

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.

Kasas
  • 1,216
  • 2
  • 18
  • 28

3 Answers3

5

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);
} 
cdelolmo
  • 66
  • 1
  • `LOCATION_PROVIDERS_ALLOWED` is deprecated since API 19. Doc sais to use `LOCATION_MODE` and `MODE_CHANGED_ACTION` (or `PROVIDERS_CHANGED_ACTION`) – Sunshinator Feb 10 '17 at 16:07
0

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;
}
rayryeng
  • 102,964
  • 22
  • 184
  • 193
0

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.

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • your solution returns false everytime, what is going wrong ? – Zhar Nov 13 '19 at 12:01
  • @Zhar I can't tell you what is going wrong there, what about the location, is it opened, and in setting, it's showing that it gets the location with what? also please refer to this answer for more details: https://stackoverflow.com/a/47258015/1638739 – Muhammed Refaat Nov 14 '19 at 06:50
  • May i suggest you to read my post https://stackoverflow.com/questions/58837839/android-broadcast-gps-turned-on-update-location ? google phone isn't cheap... I already read your answer but i want my app works and does not have a dependency about location method so how to achieve that ? – Zhar Nov 14 '19 at 11:31