Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Location Mode to High Accuracy Programmatically Android

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.

like image 743
ik024 Avatar asked Aug 07 '14 06:08

ik024


People also ask

How do I fix location accuracy on Android?

Turn On Google Location Accuracy To enable Google Location Accuracy on your Android phone, follow the steps below. Step 1: Open the Settings app on your phone and tap Location. Step 2: Go to Location services and tap on Google Location Accuracy. Step 3: Toggle on the switch next to Improve Location Accuracy.

Where is high accuracy location Android?

Open your device's Settings app. Tap Security & location Location Mode. Choose High accuracy or Battery saving. Both options can use Wi-Fi, mobile networks, and sensors to determine location.


3 Answers

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));
like image 60
kaolick Avatar answered Oct 12 '22 15:10

kaolick


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)

like image 3
userM1433372 Avatar answered Oct 12 '22 13:10

userM1433372


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.

like image 2
Magnus Avatar answered Oct 12 '22 15:10

Magnus