Search code examples
androidgpslocationandroid-locationlocationlistener

OnLocationChanged not fired on some devices


I'm aware that there's a lot of 'similar' questions, but none of them provided a useful answer.

I am trying to listen to location updates, both from network and GPS. The LocationListener initialization is pretty standard:

locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 15000, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000, 0, locationListener);

I tested it on two devices, Samsung Galaxy S4 (SM-N910F) and LG G Flex 2 (H950). On the first device onLocationChanged is called as expected (only from 'network' provider), while the event is never fired on the second.

Both devices are set on 'High Accuracy' mode. I tried the second device on Wi-Fi as well as on 4G LTE connection. Since I use Google Maps API, I can clearly see the user marker moving on map, yet the event is never called.

Any idea why onLocationUpdated is never called on the second device?

Additionally, I read that GPS takes a very long time to fix. Is there any point in requesting updates from the GPS provider?

Thanks!


Solution

  • May I suggest you start using Google Play location APIs.

    The Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app. If you are currently using the Android framework location APIs, you are strongly encouraged to switch to the Google Play services location APIs as soon as possible.

    https://developer.android.com/training/location/index.html