18

I have an android app that uses location. But I noticed that if users disable the 'Access to my location' in Settings > Location access, nothing works anymore. How can I check that it's enabled? In case it's disabled, how to open those settings from my app?

Thanks

SOLVED :

String locationProviders = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (locationProviders == null || locationProviders.equals("")) {
    ...
    startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
Romain Pellerin
  • 2,470
  • 3
  • 27
  • 36

5 Answers5

8

You can check it like that:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) // Return a boolean

EDIT:

If you want to check the network provider:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) // Return a boolean

EDIT 2:

If you want to open the settings, you can use this intent:

Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
Brtle
  • 2,297
  • 1
  • 19
  • 26
  • You can too check the availability of the network provider (replace `LocationManager.GPS_PROVIDER` by `LocationManager.NETWORK_PROVIDER`). – Brtle Aug 09 '13 at 16:01
  • 1
    If you want Wifi use the NETWORK provider. Have a look at docs and official Android articles, such as: http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html (and note there is newer stuff now too, via Google Play Services, like the fused provider: https://developer.android.com/google/play-services/location.html. – Charlie Collins Aug 09 '13 at 16:02
  • Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); works better as it provides every provider available – Romain Pellerin Aug 09 '13 at 16:02
  • Thanks Charlie Collings, I'll have a look at fused provider – Romain Pellerin Aug 09 '13 at 16:04
  • 1
    I never used `Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);` but I will try it. In addition, I put a clue to open the location source settings. – Brtle Aug 09 '13 at 16:15
3

may be this will be useful check this site it discusses about location service

http://www.scotthelme.co.uk/android-location-services/

Gavriel
  • 18,880
  • 12
  • 68
  • 105
Prakhar
  • 2,270
  • 19
  • 26
  • Ok it seems to be Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); thansk, any idea how to open it from my app? – Romain Pellerin Aug 09 '13 at 15:58
2

An alternative way to do that without using Settings.Secure and without scanning all location providers would be to do:

LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
int providersCount = locationManager.getProviders(true).size(); // Listing enabled providers only
if (providersCount == 0) {
    // No location providers at all, location is off
} 
Vincent Hiribarren
  • 5,254
  • 2
  • 41
  • 65
0

Unfortunately, it seems the using of Settings.Secure.LOCATION_PROVIDERS_ALLOWED is deprecated since API 19.

A new way to do that would be:

int locationMode = Settings.Secure.getInt(
    getContentResolver(),
    Settings.Secure.LOCATION_MODE,
    Settings.Secure.LOCATION_MODE_OFF // Default value if not found
);

if (locationMode == Settings.Secure.LOCATION_MODE_OFF) {
    // Location is off
}
Vincent Hiribarren
  • 5,254
  • 2
  • 41
  • 65
-1

There are two ways to check location, 1-using GPS 2-using network provider its better to check both service are enabled or not.For that use this method :)

public boolean checkServices(){
    //check location service
    LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) &&
            locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
            return true;
    }
}
Ashana.Jackol
  • 3,064
  • 28
  • 22
  • 1
    i dont know why some one gives me -1 for this...this solution is works for me and im using it.if the solution didnt work with you try another solution dont mark as -1 idiot, – Ashana.Jackol Sep 08 '17 at 05:44