35

Is it possible to get the information on the location mode which the user has selected among the three modes under the location settings options i.e

1.Hight Accuracy

2.Battery Saving

3.GPS Only

I want to programmatically check if user has selected High Accuracy mode if not then enable it automatically. Is it possible ? Please advice.

Rhys
  • 4,511
  • 2
  • 23
  • 32
ik024
  • 3,566
  • 7
  • 38
  • 61
  • i was searching for something similar; to enable it, for developers, ...as close as it gets to automatically, [found that it would be a part of the Google Play Services 7.0](http://stackoverflow.com/questions/28759454/enabling-location-with-mode-high-accuracy-or-battery-saving-without-user-needing/29002760#29002760) – Pararth Mar 12 '15 at 06:08
  • possible duplicate of [Android: detect when GPS is turned on/off (or when no app is using it anymore)](http://stackoverflow.com/questions/15452084/android-detect-when-gps-is-turned-on-off-or-when-no-app-is-using-it-anymore) – Marian Paździoch Apr 13 '15 at 07:54

3 Answers3

64

It is possible to get the device's current location mode since API level 19 (Kitkat):

public int getLocationMode(Context context) {
    return Settings.Secure.getInt(activityUnderTest.getContentResolver(), Settings.Secure.LOCATION_MODE);
}

These are the possible return values (see here):

0 = LOCATION_MODE_OFF  
1 = LOCATION_MODE_SENSORS_ONLY  
2 = LOCATION_MODE_BATTERY_SAVING  
3 = LOCATION_MODE_HIGH_ACCURACY

So you want something like

if(getLocationMode(context) == 3) {
    // do stuff
}

Unfortunately you can't set the location mode programmatically but you can send the user directly to the settings screen where he can do that:

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
JunYao Yuan
  • 223
  • 1
  • 6
kaolick
  • 4,817
  • 5
  • 42
  • 53
  • Are you sure you can't change the location mode programmatically? I am trying to do something similar and would like to automatically change them if I can. So if you're sure it's impossible, that would be nice to know. – BlackThorn Feb 24 '17 at 20:41
  • 2
    @TBear Yeah, pretty shure. I don't think that this kind of global setting should be allowed to be manipulated by an app. – kaolick Feb 27 '17 at 07:25
  • 1
    LOCATION_MODE_... is now deprecated. – Mohammad Reza Lohrasbi Feb 19 '20 at 12:53
3

You can provide criteria in the LocationMamager.requestLocationUpdates. As criteria you can provide one of the following values to select the accuracy needed.

Constants
int ACCURACY_COARSE A constant indicating an approximate accuracy requirement
int ACCURACY_FINE   A constant indicating a finer location accuracy requirement
int ACCURACY_HIGH   a constant indicating a high accuracy requirement - may be used for horizontal, altitude, speed or bearing accuracy.
int ACCURACY_LOW    A constant indicating a low location accuracy requirement - may be used for horizontal, altitude, speed or bearing accuracy.
int ACCURACY_MEDIUM A constant indicating a medium accuracy requirement - currently used only for horizontal accuracy.

See http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(long, float, android.location.Criteria, android.app.PendingIntent)

userM1433372
  • 5,345
  • 35
  • 38
2

Since API level 28, you should use LocationManager.isProviderEnabled() to find out if a specific provider is enabled or not.

Changing the setting programmatically from your app is unfortunately not possible.

Magnus
  • 17,157
  • 19
  • 104
  • 189
  • 1
    It's not about is GPS on or not; It's about it's on but it is set to Battery saving mode, for example. – Dr.jacky Apr 25 '19 at 09:36