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