-1

I tried to get a location for my android app but getLastKnownLocation always returns null and onLocationChanged is never called. I use the following permissions for my app:

ACCESS_NETWORK_STATE, 
ACCESS_FINE_LOCATION ACCESS_COARSE_LOCATION. 

GPS and internet are turned on, on my device on. Also I tried different values for requestLocationUpdates.

        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        locationManager.removeUpdates(this);

        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) 
        {
            // no network provider is enabled
        } 
        else 
        {
            hasGPS      = true;
            location    = null;
            if (isGPSEnabled) 
            {
                if (location == null) 
                {
                    locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if (locationManager != null) 
                    {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) 
                        {
                            lat = location.getLatitude();
                            lng = location.getLongitude();
                        }
                    }
                }
            }

            if (isNetworkEnabled && location == null) 
            {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) 
                {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) 
                    {
                        lat = location.getLatitude();
                        lng = location.getLongitude();
                    }
                }
            }
        }
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
  • This may sounds silly, but you're testing this on an actual handset correct (ie, not using the emulator)? – jedwards May 12 '14 at 16:31
  • Relateds that may help [here](http://stackoverflow.com/questions/19621882/getlastknownlocation-returning-null) and [here](http://stackoverflow.com/questions/1608632/android-locationmanager-getlastknownlocation-returns-null). – jedwards May 12 '14 at 16:33
  • yes i tried this code on a tablet with android 4.1 – user3629312 May 12 '14 at 16:33
  • Install a well-known Android app where you can see whether GPS is functional. BTW where is your listener for the location updates? – hgoebl May 12 '14 at 16:41

2 Answers2

0

GetLastKnownLocation will return null if it either never has had a location via that provider or if the location is too old. So that isn't unexpected. Most likely you aren't getting a GPS signal, which can take several seconds-several minutes (or fail altogether in a lot of buildings). Double check that GPS is turned on in settings, and look at the flashing circle in the notification area- if it never becomes solid, then you aren't getting a signal.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

GPS signal cannot penetrate through walls, so it usually does not work inside buildings.

For developing and testing purposes you can use Android mock locations: http://developer.android.com/training/location/location-testing.html

Anton Tananaev
  • 2,458
  • 1
  • 25
  • 48