17

I've got this code

 lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
 boolean isGPS = lm.isProviderEnabled (LocationManager.GPS_PROVIDER);

and it always return false, even when GPS is enabled. GPS is working correctly but I'm using this boolean value to show pop-up "No GPS enabled". In this situation pop-up is showing up every time

  1. I checked similar questions but it doesn't help me.
  2. Yes, i have permission in my manifest
  3. I'm using this code in onResume method

Thanks for help.

Lau
  • 1,804
  • 1
  • 23
  • 49

6 Answers6

40

If you are checking for the location is opened or not whatever it is using GPS or not, so you have to pay attention to the following as it was my case, in device location settings the locating method was set to BatterySaving mode, in which the device uses only WiFi mobile networks to estimate location:

enter image description here

Therefore, the GPS is not even used in update location, and so the location icon will not appear in the status bar in addition to the location provider will be stated as Network not GPS.

so, to solve that issue you have to check whether the provider contains gps or contains network:

private boolean checkIfLocationOpened() {
    String provider = Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (provider.contains("gps") || provider.contains("network"))
        return true;
    }
    // otherwise return false
    return false;
}

and if you want to do it using the LocationManager:

private boolean checkIfLocationOpened() {
    final LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) || manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        return true;
    }
    // otherwise return false
    return false;
}
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • @MuhammedRefaat But it still returns false for both `manager.isProviderEnabled(LocationManager.GPS_PROVIDER)` & `manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)` when the GPS is off! I've tested it on Android API 28. – Dr.jacky Apr 24 '19 at 11:14
  • 1
    @Dr.jacky if location settings has been set to 'High Accuracy' and the GPS is off it will return off offcourse, are you sure this is not your scenario? – Muhammed Refaat Apr 24 '19 at 13:56
  • @MuhammedRefaat But in the hint, it says: "Uses GPS, Wi-Fi, and mobile networks to estimate your location". So it could get the location from Network if the GPS is off! -> https://i.stack.imgur.com/fKr3H.png – Dr.jacky Apr 25 '19 at 09:30
  • 1
    @Dr.jacky yes you are right, so it should use network, let me check from my side and will tell you if I could reach something – Muhammed Refaat Apr 25 '19 at 10:35
  • @Gerrard My pleasure – Muhammed Refaat Apr 19 '20 at 06:14
11

try this way..

private void turnGPSOn(){   
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);   
if(!provider.contains("gps")){      
    final Intent poke = new Intent();  
    poke.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");           
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);   
    poke.setData(Uri.parse("3"));      
    sendBroadcast(poke);  
    } 
}  
Dinithe Pieris
  • 1,822
  • 3
  • 32
  • 44
  • I used a part of your code. This 'if' return true value – Lau Feb 06 '14 at 10:42
  • Is their any reason why using Settings.Secure would return better result than LocationManager.isProviderEnabled? I'm asking because it seems that on some device isProvderEnabled doesn't work as expected... – Gyome Nov 25 '15 at 19:04
  • with GPS open, it my provider value is Network, plus now LOCATION_PROVIDERS_ALLOWED is deprecated. Is there another solution ? – Aalap Patel May 09 '18 at 02:42
2

some time location manager returns the wrong result.below code works for me.

public static boolean isGpsEnabled(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            String providers = Settings.Secure.getString(context.getContentResolver(),
                    Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            if (TextUtils.isEmpty(providers)) {
                return false;
            }
            return providers.contains(LocationManager.GPS_PROVIDER);
        } else {
            final int locationMode;
            try {
                locationMode = Settings.Secure.getInt(context.getContentResolver(),
                        Settings.Secure.LOCATION_MODE);
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
                return false;
            }
            switch (locationMode) {

                case Settings.Secure.LOCATION_MODE_HIGH_ACCURACY:
                case Settings.Secure.LOCATION_MODE_SENSORS_ONLY:
                    return true;
                case Settings.Secure.LOCATION_MODE_BATTERY_SAVING:
                case Settings.Secure.LOCATION_MODE_OFF:
                default:
                    return false;
            }
        }
    }
0

Try this GPS Example code. Hope it should helpful for you.

GPS Location

Jebasuthan
  • 5,538
  • 6
  • 35
  • 55
0

Sometime you have to restart your device, maybe gps-status return fail that time.

Huỳnh Ngọc Bang
  • 1,572
  • 1
  • 19
  • 22
0

To fix GPS isProviderEnabled Always returns false on Android 6, add this to your manifest:

<uses-feature android:name="android.hardware.location.gps" />

Titanium cannot find GPS hardware on android 5.0 & above, thus it keeps returning false. add above line to fix issue.