'Android LocationManager locationManager.requestLocationUpdates() not working

hi guys i am trying to get current location with location manager, i am using this below code

LocationManager locationManager = (LocationManager) 
getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider =locationManager.getBestProvider(criteria,true);

        Location location = 
locationManager.getLastKnownLocation(bestProvider);
        if (location != null) {
            onLocationChanged(location);
        }
        locationManager.requestLocationUpdates(bestProvider, 20000, 0,this);

But i am not getting any response for this line

locationManager.requestLocationUpdates(bestProvider, 20000, 0,this);

and also getting lastknown location as null, i have added all permissions in manifest as well as dynamic, but this above line is not giving any response, i searched for it but dint get and relevant answer , please help.



Solution 1:[1]

For the getLastKnownLocation method, it is said in the documentation :

If the provider is currently disabled, null is returned.

Which means your GPS is disabled.

For the requestLocationUpdates, you are requesting a location every 20 seconds, try to decrease this number so you know at least if your program is working.

You can try this :

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

Also I really advise you to use the new API to request location updates : https://developer.android.com/training/location/receive-location-updates.html

It is very fast and accurate, but you need to create a google developer account (for free) and create an api key. All needed information should be in the link.

Solution 2:[2]

String bestProvider =locationManager.getBestProvider(criteria,true);

In the above line, the second boolean parameter is true. That means it will fetch the provider which is enabled. And you the GPS_PROVIDER is disabled which is validated by the fact that locationManager.getLastKnownLocation returns null.

Moreover, since your Criteria for requesting BestProvier is empty so you must be getting "passive" as your BestProvider. Which means you will receive location updates only when other applications request and receives updates.

To get real location updates in onLocationChangedMethod() of your class. You must ensure these things:

  1. Make sure that you have either one of ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION and then request it in runtime too, which you say you have already done.
  2. If you want to request location through GPS_PROVIDER then you need to make sure that GPS_PROVIDER is enabled you can do that using below code:

    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); // OR some other PRIORITY depending upon your requirement
    
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);
    
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(googleApiClient,
                    builder.build());
    
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult locationSettingsResult) {
            final Status status = locationSettingsResult.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All location settings are satisfied. The client can
                    // Request for location updates now
    
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied, but this can be fixed
                    // by showing the user a dialog.
                    status.startResolutionForResult(YourActivity.this, 100); // First parameter is your activity instance
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way
                    // to fix the settings so we won't show the dialog.
                    Log.e("TAG", "Error: Can't enable location updates SETTINGS_CHANGE_UNAVAILABLE");
                    break;
            }
        }
    });
    

Solution 3:[3]

LocationManager.NETWORK_PROVIDER need network, but it is real, not precise. if you want to use LocationManager.GPS_PROVIDER, the situation must be outdoor instead of indoor, because GPS location need satellite, if you are in any building, the satellite cannot find you! pleasle go outdoor and check with GPS_PROVIDER again!

*best approach is getting location from both of GPS and NETWORK and check any of then that was not null and more accurate useing it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 ankit209
Solution 3 Mohammad Reisi