Obtaining location becomes an important feature of today’s applications.
We can get location via two providers

  • GPS Location Provider

    • Obtain user location from satellite
    • More accurate
    • Only works outdoors
    • Consumes battery power
    • Takes much time to get the location
  • Network Location Provider

    • Obtain user location from Cell tower and wifi signals
    • Works indoors, outdoors
    • Responds faster than GPS
    • Consumed less power

Obtain location object
The first step is to get reference to location manager
we can check for available providers via getAllProviders method, this method returns a List of all available providers

another important method is getLastKnownLocation that returns Location object holds all location information, this is the last measured location(Cached) by a specific provider
this method takes the type of provider as an argument

        // get Location manager
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // List all Location providers, usually Network, GPS
        List providers = locationManager.getAllProviders();
        Location location = null;
        // Check if available providers contains GPS (More accurate)
        	// get location from the last GPS Location detection
        	location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        // Check if available providers contains Network provider
        else if(location == null || providers.contains(LocationManager.NETWORK_PROVIDER))
        	// get location from the last Network Location detection
        	location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

the location object holds all location information
like location coordinates via methods

  • getAltitude
  • getLatitude
  • getLongitude

and the accuracy via getAccuracy and the provider via getProvider and the time via getTime

In order to make a location aware application you should request a permission in AndroidManifest.xml

Listen for location changes
in order to listen for changes in location you should implement the interface LocationListener
the most important method in the listener is onLocationChanged method that fires on every change in location providing a Location argument holds the required location data.

LocationListener locationListener = new LocationListener() {
			public void onStatusChanged(String provider, int status, Bundle extras) {

			public void onProviderEnabled(String provider) {

			public void onProviderDisabled(String provider) {
			// when the location changes
			public void onLocationChanged(Location location) {


To start listening for location updates you should tell the Location manager to tell the listener about any updates in location.
The listener takes four arguments

  1. Location Provider
    a string value defines the type of location provider
    one of two values: LocationManager.NETWORK_PROVIDER, LocationManager.GPS_PROVIDER
  2. Minimum Time
    long value that defines the minimum time to inform the listener about location updates.
  3. Minimum Distance
    long value that define the minimum distance to inform the listener about location updates
  4. Location Listener

you can set minimum time and distance to zero to get all updates as possible but this may consume more power.

	// get all update in location from GPS
	locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
	// get all update in location from Network provider
	locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);