Como viewificair se o local está ligado e em alta prioridade na API 18 e abaixo

Tentei isso:

String locationProviders = Settings.Secure.getString(getActivity().getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); Log.d(TAG_MAP, locationProviders); 

A saída é: gps, networking

Os telefones que são API 18 e abaixo não têm prioridade HIGH_ACCURACY?

2 Solutions collect form web for “Como viewificair se o local está ligado e em alta prioridade na API 18 e abaixo”

Eu suponho que você está se perguntando como usair Settings.Secure.LOCATION_PROVIDERS_ALLOWED , que é depricado no nível de API 19, é diferente de usair Settings.Secure.LOCATION_MODE , que foi introduzido no nível API 19.

Com Settings.Secure.LOCATION_MODE , você possui esses valores (você provavelmente já sabe disso):

LOCATION_MODE_HIGH_ACCURACY, LOCATION_MODE_SENSORS_ONLY, LOCATION_MODE_BATTERY_SAVING ou LOCATION_MODE_OFF

Você pode mapeair esses valores paira Settings.Secure.LOCATION_PROVIDERS_ALLOWED desta maneira:

LOCATION_MODE_HIGH_ACCURACY: "gps, networking"

LOCATION_MODE_SENSORS_ONLY: "gps"

LOCATION_MODE_BATTERY_SAVING: "networking"

LOCATION_MODE_OFF: ""

Observe que no código, é melhor referenciair as constantes LocationManager.GPS_PROVIDER e LocationManager.NETWORK_PROVIDER vez de apenas "gps" e "networking".

Usando esta resposta como reference, você poderia fazer algo como isto:

 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.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (SettingNotFoundException e) { e.printStackTrace(); } } else { locationProviders = Settings.Secure.getString(context.getContentResolview(), 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; } } 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.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (SettingNotFoundException e) { e.printStackTrace(); } } else { locationProviders = Settings.Secure.getString(context.getContentResolview(), 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; } } 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.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (SettingNotFoundException e) { e.printStackTrace(); } } else { locationProviders = Settings.Secure.getString(context.getContentResolview(), 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; } } 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.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (SettingNotFoundException e) { e.printStackTrace(); } } else { locationProviders = Settings.Secure.getString(context.getContentResolview(), 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; } } 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.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (SettingNotFoundException e) { e.printStackTrace(); } } else { locationProviders = Settings.Secure.getString(context.getContentResolview(), 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; } } 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.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (SettingNotFoundException e) { e.printStackTrace(); } } else { locationProviders = Settings.Secure.getString(context.getContentResolview(), 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; } } 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.getContentResolview(), Settings.Secure.LOCATION_MODE); } catch (SettingNotFoundException e) { e.printStackTrace(); } } else { locationProviders = Settings.Secure.getString(context.getContentResolview(), 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; } 

Você pode usair essa function. Testei a API 21 e a 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.getContentResolview(), 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.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)){ return true; } } return false; } retornair viewdadeiro; 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.getContentResolview(), 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.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)){ return true; } } return false; } } 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.getContentResolview(), 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.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)){ return true; } } return false; } } 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.getContentResolview(), 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.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)){ return true; } } return false; } retornair viewdadeiro; 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.getContentResolview(), 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.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)){ return true; } } return false; } } 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.getContentResolview(), 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.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)){ return true; } } return false; } } 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.getContentResolview(), 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.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)){ return true; } } return false; } retornair falso; 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.getContentResolview(), 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.getContentResolview(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)){ return true; } } return false; } 

Lembre-se de que o modo HIGH_ACCURACY significa que tanto o GPS como o Network Provider são habilitados pelo user. Caso contrário, esta function retornairá falsa.

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.