2

I got a problem when using LocationManager requestLocationUpdates on Lollipop device. The LocationListener() callback doesn't get called. Also I've tried to use FusedLocationProviderClient api, but also the callback doesn't work. (All dependency like permissions, Manifest, GPS are already ON)

This is my code for LocationManager

LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        Log.d("test", "GOTCHA");
                    }

                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {

                    }

                    @Override
                    public void onProviderEnabled(String provider) {

                    }

                    @Override
                    public void onProviderDisabled(String provider) {

                    }
                });

This is for FusedLocationProviderClient

FusedLocationProviderClient fusedLocationProviderClient = new FusedLocationProviderClient(getContext());
    LocationCallback mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);
            Log.d("test", "GOTCHA");
            if (locationResult != null) {
                Location currentLocation = locationResult.getLastLocation();
                handleCurrentLocation(currentLocation);
                fusedLocationProviderClient.removeLocationUpdates(this);
            } else {
                failToGetLocationHandler("Fail to initiate request location ...");
            }
        }

    };

    fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);

I've tried to look every where for solutions but no luck for me. But the thing is, it works on other Android version like Nougat / Oreo. As far as I know, these 2 APIs don't have any problem with Lollipop (please CMIIW).

Thanks

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Webster
  • 1,113
  • 2
  • 19
  • 39
  • 1
    You should use google api client for continuous updates and accurate results – Quick learner Jul 13 '18 at 06:20
  • 1
    GPS probably won't work indoors, network based location provider might require a SIM card (I'm not sure) and it's perfectly legal to use `LocationManager` although the newer fancier APIs are of course recommended by Google. And of course if you are testing with several different devices, some may have better GPS chips and antennas than others, so test outside or close to an open balcony door or something like that. – Markus Kauppinen Jul 13 '18 at 07:19
  • Hi guys, thanks for your time. Appreciate it. Somehow the emulator that I use, it just doesn't work. but when I tried on real device, it works. I don't know what was the issue, I suspect it was hardware related. – Webster Jul 13 '18 at 11:47

0 Answers0