Change Location Mode to High Accuracy Programmatically Android

ik024 picture ik024 · Aug 7, 2014 · Viewed 27.2k times · Source

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.

Answer

kaolick picture kaolick · Sep 9, 2014

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));