android.location package

Location Awareness in Android part I

Posted by Romina Liuzzi on January 8, 2018

To get the user current location and request location updates from your Android app you could use the SDK embadded android.location package.

As these classes are part of the Android SDK there is no prior setup required and you don't need to add any dependencies to your gradle file.

Warning

image-title-here

Google strongly advises to stop using this API and migrate your apps to use Google Location Services APIs instead.


To learn the right way to do this checkout Google Location Services APIs Post


Location awareness using android.location package

You will need to get familiar with the classes:

  • LocationProvider
  • LocationManager
  • LocationListener

The plan is to attach a LocationListener to a LocationManager for a given LocationProvider (NETWORK_PROVIDER, GPS_PROVIDER,...) and listen for updates.

	String locationProvider = LocationManager.NETWORK_PROVIDER;
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

There are some considerations to keep in mind when using this approach like what provider to choose (i.e: GPS is more accurate but doesn't work indoors).

You also need to consider every how often your updates should happen and how accurate they should be. These decisions will have an impact on the battery life of the device your app is running on so you have to be careful.

References

Android Developer Tutorial on Location Strategies