Cómo comprobar si la ubicación está en alta prioridad y en la API 18 y siguientes
Probado esto:
String locationProviders = Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); Log.d(TAG_MAP, locationProviders);
La salida es: gps, red
¿Son los teléfonos que son API 18 e inferiores no tienen prioridad HIGH_ACCURACY?
Supongo que usted se está preguntando cómo usar Settings.Secure.LOCATION_PROVIDERS_ALLOWED
, que es depricated en el nivel 19 de la API, es diferente de usar Settings.Secure.LOCATION_MODE
, que se introdujo en el nivel 19 de la API.
Con Settings.Secure.LOCATION_MODE
, tiene estos valores (probablemente ya lo sabe):
LOCATION_MODE_HIGH_ACCURACY, LOCATION_MODE_SENSORS_ONLY, LOCATION_MODE_BATTERY_SAVING o LOCATION_MODE_OFF
Puede asignar estos valores a Settings.Secure.LOCATION_PROVIDERS_ALLOWED
de esta manera:
LOCATION_MODE_HIGH_ACCURACY: "gps, red"
LOCATION_MODE_SENSORS_ONLY: "gps"
LOCATION_MODE_BATTERY_SAVING: "red"
LOCATION_MODE_OFF: ""
Tenga en cuenta que en el código, es mejor hacer referencia a las constantes LocationManager.GPS_PROVIDER
y LocationManager.NETWORK_PROVIDER
lugar de simplemente "gps" y "network".
Utilizando esta respuesta como referencia, podría hacer algo como esto:
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; }
Puede utilizar esta función. He probado en API 21 y API 16
public static boolean checkHighAccuracyLocationMode(Context context) { int locationMode = 0; String locationProviders; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ //Equal or higher than API 19/KitKat try { locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE); if (locationMode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY){ return true; } } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } }else{ //Lower than API 19 locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)){ return true; } } return false; }
Recuerde que el modo HIGH_ACCURACY significa que tanto el GPS como el proveedor de red están habilitados por el usuario. De lo contrario esta función devolverá false.