Tried this:
String locationProviders = Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.d(TAG_MAP, locationProviders);
Output is: gps,network
Are phones that are API 18 and below dont' have HIGH_ACCURACY Priority?
Settings > Location > Mode > Battery Saving . This mode only uses WiFi, Bluetooth or mobile data instead of GPS to determine the user location. That's why you have to check if the network provider is enabled and locationManager.
First, create a Location Manager object by calling the getSystemService() method and LOCATION_SERVICE as an argument. Call getBestProvider() to get the location in the form of a string. Now the provider to getLastKnownLocation() as an argument to get the location.
This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to be notified when the device enters the proximity of a given geographical location.
I assume you're wondering how using Settings.Secure.LOCATION_PROVIDERS_ALLOWED
, which is depricated in API level 19, is different from using Settings.Secure.LOCATION_MODE
, which was introduced in API level 19.
With Settings.Secure.LOCATION_MODE
, you have these values (you probably already know this):
LOCATION_MODE_HIGH_ACCURACY, LOCATION_MODE_SENSORS_ONLY, LOCATION_MODE_BATTERY_SAVING, or LOCATION_MODE_OFF
You can map these values to Settings.Secure.LOCATION_PROVIDERS_ALLOWED
in this way:
LOCATION_MODE_HIGH_ACCURACY : "gps,network"
LOCATION_MODE_SENSORS_ONLY : "gps"
LOCATION_MODE_BATTERY_SAVING : "network"
LOCATION_MODE_OFF : ""
Note that in code, it's best to reference constants LocationManager.GPS_PROVIDER
and LocationManager.NETWORK_PROVIDER
instead of just "gps" and "network".
Using this answer as a reference, you could do something like this:
public static int getLocationMode(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (TextUtils.isEmpty(locationProviders)) {
locationMode = Settings.Secure.LOCATION_MODE_OFF;
}
else if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)) {
locationMode = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
}
else if (locationProviders.contains(LocationManager.GPS_PROVIDER)) {
locationMode = Settings.Secure.LOCATION_MODE_SENSORS_ONLY;
}
else if (locationProviders.contains(LocationManager.NETWORK_PROVIDER)) {
locationMode = Settings.Secure.LOCATION_MODE_BATTERY_SAVING;
}
}
return locationMode;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With