javageolocationandroid-locationandroid-gps

Location is not returning anything for API below 23 (APIs 21 and 22)


My location service is working very well on APIs >=23 but it show nothing for APIs 21 and 22. This is my location method (Note that I am using fragment):

/*if (!Permissions) { //location permission for APIs >=23
                Log.d("Location", "Unable to get user location");
                return;
            } */
                Log.d("Location", "User location requesting ... ");
                LocationServices.getFusedLocationProviderClient(fragment.requireActivity())
                        .requestLocationUpdates(requestLocation(), new LocationCallback() {
                            @Override
                            public void onLocationResult(@NonNull LocationResult locationResult) {
                                super.onLocationResult(locationResult);
                                Log.d("User location", Objects.requireNonNull(locationResult.getLastLocation()).toString());
                            }
                        }, null);
        });

and this is the requestLocation() method that I've used it in above:

private static LocationRequest requestLocation() {
            return new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 5000)
                    .setWaitForAccurateLocation(true)
                    .setMinUpdateIntervalMillis(2000)
                    .setMaxUpdateDelayMillis(100)
                    .setMaxUpdates(1)
                    .build();
        }

But this is not working for APIs 21 and 22. I've tried to set location manually in emulator setting, but again nothing happened. Any suggestion (non-deprecated of course) to how to get user location in java for these APIs?

I also tried to use the "last location" method but it didn't help.

Here are some SO related posts.

How do I get the current GPS location programmatically in Android?

How to get current location in Android


Solution

  • Finally after many search, I figured out the problem. The FusedLocationProviderClient method only works on systems that have GooglePlayStore installed. It is not related to API levels 22 or 21.