Obtaining Location Updates in Android
I was trying to obtain location information in an Android application that I'm developing and it didn't work using the open-source Android Support library. Here's how I fixed it.
The Problem
I was using the requestLocationUpdates(...) method in the LocationManager class, to register for location updates within the activity. The activity inherited the LocationListener interface and implemented the following method.
The manifest file was updated to obtain the ACCESS_FINE_LOCATION permission and everything looked fine with my code. But after installing the application on my Galaxy Nexus (Android v4.3), I wasn't obtaining any location information. The onLocationChanged(...) method was not getting invoked at all. I tried changing the provider from GPS_PROVIDER to NETWORK_PROVIDER to obtain coarse location information and that didn't work either. This seems to be a common problem and none of the solutions in the StackOverflow threads here, here and here helped. But here's what worked...
The Solution
Switching to the new Google Play services library. This library is supported in Android v2.2 and above, which supposedly accounts for about 98% of active Android users. A guide to install and setup the library can be found here. The LocationClient class can then be used to start/stop the service and also request for location updates. The activity class has to now implement Google's new LocationListener. The onLocationChanged(...) method is the same as the snippet above. Google has sure cleaned up a lot of its location APIs through the new proprietary library. For instance, the fused location provider obfuscates the need for defining the provider (either GPS or Network) in code. The library takes care of choosing the appropriate provider based on the LocationRequest. The geofencing and activity recognition features are also very cool.
I however have not understood why the old LocationManager in the Android Support library doesn't work. Is this Google's ploy of forcing all developers to use their proprietary system?