15

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?

Jan
  • 3,393
  • 4
  • 21
  • 47

3 Answers3

23

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;
} 
vman
  • 1,264
  • 11
  • 20
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
4

You can use this function. I tested on API 21 and 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;
}

Remember that HIGH_ACCURACY mode means both GPS and Network Provider is enabled by the user. Otherwise this function will return false.

Ghulam Ali
  • 1,935
  • 14
  • 15
0

Try this Settings.Secure.LOCATION_MODE

String gpsStatus = android.provider.Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE);

You can use this checking

if (Build.VERSION.SDK_INT >= 21 &&
        ActivityCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
        ActivityCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    Log.e("gpsStatus", "LOCATION PERMISSION NOT GRANTED");
} else {

    Log.d("gpsStatus", "LOCATION PERMISSION GRANTED");

    if (gpsStatus.indexOf("gps", 0) < 3) {
        Log.d("gpsCheck", "DEVICE LOCATION PERMISSION IS GRANTED");
     
    } else {
        Log.e("gpsCheck", "DEVICE LOCATION PERMISSION NOT GRANTED");
    }
}
Jerald Jacob
  • 77
  • 2
  • 10