Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Checking if google settings location is enabled

I'm working on an app that uses google play services. On Some phones, the client returns null for location. This happens because the locaiton option is not enabled on google setting as in the pic attached.

How to programatically check if google settings location is enabled in an android app ?

http://www.cnet.com/how-to/explaining-the-google-settings-icon-on-your-android-device/

like image 923
Moataz Soliman Avatar asked Jun 05 '14 18:06

Moataz Soliman


People also ask

How do I know if my location is 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.

Does Google need location permission all the time?

Select an option: All the time: The app can use your location at any time. Only while using the app: The app can use your location only when you're using that app. Ask every time: Every time you open the app, it'll ask to use your location.


2 Answers

It's possible to check if google play services itself is available. Follow instructions here: https://developer.android.com/training/location/retrieve-current.html

It's possible getLastLocation() will return null even if Google Play Services Location Services are enabled. For instance, right after re-enabling them. However you can then take your user to the Google Play Services Location Settings by using the following intent:

Intent settings = new Intent("com.google.android.gms.location.settings.GOOGLE_LOCATION_SETTINGS");
startActivity(settings);

Finally, it's also possible to check if Android Location Services are enabled:

public static boolean isLocationEnabled(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();
        }

        return locationMode != Settings.Secure.LOCATION_MODE_OFF;

    }else{
        locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        return !TextUtils.isEmpty(locationProviders);
    }


} 
like image 75
gflarity Avatar answered Nov 08 '22 05:11

gflarity


I thought this is the best solution.

You can check by following code :

    /**
     * This is used to check whether location is enabled or not.
     * @param mContext
     * @return
     */
    public static boolean isDeviceLocationEnabled(Context mContext) {
        int locMode = 0;
        String locProviders;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            try {
                locMode = Settings.Secure.getInt(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE);
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
                return false;
            }
            return locMode != Settings.Secure.LOCATION_MODE_OFF;
        }else{
            locProviders = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            return !TextUtils.isEmpty(locProviders);
        }
    }
like image 1
Aj 27 Avatar answered Nov 08 '22 06:11

Aj 27