6

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/

2
  • use LocationManager to check the different providers
    – tyczj
    Jun 5, 2014 at 18:21
  • It's possible to check if google play services itself is available. Follow instructions here: 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 this Intent.
    – anon
    Nov 16, 2014 at 18:31

2 Answers 2

1

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);
        }
    }
0

There should be one change, where you check TextUtils.isEmpty(locationProviders). It is never empty. Where there is no provider (location settings are disabled) it has value "null". Yes I mean value "null", not null.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.