Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if location is on and in high priority on API 18 and below

Tags:

android

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?

like image 805
Jan Avatar asked Apr 23 '15 04:04

Jan


People also ask

How do you check if location services are enabled?

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.

How will you initiate location manager object?

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.

What is location manager in Android?

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.


1 Answers

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;
} 
like image 51
Daniel Nugent Avatar answered Oct 13 '22 00:10

Daniel Nugent