Understanding Android GPS and Location Manager

Understanding Android GPS and Location Manager

Nearly all Android devices posses GPS in them and you can make use of it to discover the location of your user. Location recognition applications are smarter and offer better information.

Take for instance, you are hungry; you open your android device and it tells you that a food canteen is 30 meters away from your current location and illustrates to you the precise location.

That’s the kind of thing that location recognition applications can do.

There are two ways to obtain user’s location:

Through the use of GPS

Through the use of Mobile Phone’s Network

GPS is the most correct one but it works when outdoors alone. Again, it speedily uses up the device battery. On the contrary, Network Location Provider makes use of cell tower and Wi-Fi signals and is functional both indoors and outdoors.

It responds very fast and makes use of less battery power.

If you are creating any location based app, you can make your app more smarter by discovering user’s location routinely. For this you are required to incorporate location modules in your application.

This tutorial tells you how to get this done.

Producing new Android Project

Produce a new project in Eclipse by going to File > New > Android Project and fill all the necessary details.

Open AndroidManifest.xml and add ACCESS_FINE_LOCATION (Which is made up of both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION). Again, if you are obtaining network-based location then you are required to incorporate INTERNET permission as well.

The techniques explained in this article apply to the platform location API in android.location. The Google Location Services API, a section of Google Play Services, makes available a more powerful, high-level framework that routinely takes care of location providers, user movement, and location accuracy.

It as well takes care of location update scheduling based on power usage parameters you make available. In the majority of situations, you’ll obtain better battery performance, in addition to more suitable accuracy, through the use of the Location Services API.

Being aware of where the user is permits your application to be smarter and provide better information to the user.

When creating a location-recognition application for Android, you can make use of GPS and Android’s Network Location Provider to obtain the user location.

Although GPS is most correct, it only works outdoors, it hastily uses up battery power, and doesn’t return the location as fast as the user wishes.

Android’s Network Location Provider determines user location with the use of cell tower and Wi-Fi signals, making available location information in a manner that works indoors and outdoors, responds faster, and makes use of less battery power.

To get the user location in your application, you can make use both GPS and the Network Location Provider, or only one.

Difficulties in Determining User Location

Getting user location from a mobile device can be complex. There are a lot of reasons why a location reading (irrespective of the source) can be made up of errors and be inexact. A few sources of error in the user location are:

A lot of location sources GPS, Cell-ID, and Wi-Fi can each offer a clue to users location. Determining which to make use of and trust is a matter of trade-offs in accurateness, swiftness, and battery-efficiency.

User movement

Due to the fact that the location of the user alters, you ought to account for movement by re-estimating user location every now and then.

Differing accuracy

Location calculates coming from each location source are not reliable in their accuracy. A location obtained 10 seconds ago from one source might be more accurate than the most recent location from another or same source.

These challenges can make it tricky to get an accurate user location reading. This tutorial makes available information to assist you face up to these challenges to get a dependable location reading.

It as well makes available ideas that you can make use of in your application to make available to the user with an exact and responsive geo-location experience.

Requesting Location Updates

Prior to addressing a few of the location errors already explained below is an introduction on the way you can obtain user location on Android.

Obtaining user location in Android works by means of callback. You state that you’d like to receive location updates from the LocationManager(“Location Manager”) by calling requestLocationUpdates(), passing it a LocationListener.

Your LocationListener ought to carry out a lot of callback technique that the Location Manager calls when the user location alters or when the status of the service changes.

For instance, the following code illustrates the way to specify a LocationListener and request location updates:

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.

public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

The first factor in requestLocationUpdates() is the type of location provider to use (in this instance, the Network Location Provider for cell tower and Wi-Fi based location).

You can control the frequency at which your listener receives updates with the second and third factor – the second is the minimum time interval between notifications and the third is the minimum alteration in distance between notifications – setting both to zero requests location notifications as regularly as possible.

The last parameter is your LocationListener, which obtains callbacks for location updates.

To ask for location updates from the GPS provider, alternate GPS_PROVIDER for NETWORK_PROVIDER. You can as well ask for location updates from both the GPS and the Network Location Provider by calling requestLocationUpdates() two times to one time for NETWORK_PROVIDER and once for GPS_PROVIDER.

Asking for User Permissions

In order to obtain location updates from NETWORK_PROVIDER or GPS_PROVIDER, you ought to ask for user permission by stating either the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission, correspondingly, in your Android manifest file.

For instance:

<manifest ... >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Without these permissions, your application will fail at runtime when asking for location updates.

If you are making use of both NETWORK_PROVIDER and GPS_PROVIDER, then you ought to ask for only the ACCESS_FINE_LOCATION permission, for the fact that it is made up of permission for both providers.

(Permission for ACCESS_COARSE_LOCATION is made up of permission alone for NETWORK_PROVIDER.)

Specifying a Model for the Best Performance

Location-based applications are currently commonplace, but as a result of the less than optimum exactness, user movement, the variety of techniques to get the location, and the desire to save battery, obtaining user location is complex.

To conquer the obstacles of obtaining a good user location while saving battery power, you ought to specify define a steady model that indicates the way your application obtains the user location.

This model includes when you begin and stop listening for updates and when to make use of cached location data.

Course for GETTING user location

Here’s the typical COURSE of procedures for getting the user location:

Launch application

A few time after, begin listening for updates from desired location providers. Preserve a “PRESENT best estimate” of location by deciphering new, but less precise fixes.

Stop listening for location updates.

Make use of the last best location calculation.

Knowing when to begin listening for updates

You might wish to begin listening for location updates as soon as your application is launched, or only after users activate a particular feature.

Know that long windows of listening for location fixes can use up a lot of battery power, but short periods may not permit for enough correctness.

As described above, you can start listening for updates by calling requestLocationUpdates():

String locationProvider = LocationManager.NETWORK_PROVIDER;

// Or, use GPS location data:

// String locationProvider = LocationManager.GPS_PROVIDER;

locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

Obtaining a speedy fix with the last known location. The time it takes for your location listener to obtain the first location fix is frequently too long for users wait.

Until a more precise location is made available to your location listener, you ought to make use of a cached location by invoking getLastKnownLocation(String):

String locationProvider = LocationManager.NETWORK_PROVIDER;

// Or use LocationManager.GPS_PROVIDER

Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

Choosing when to stop listening for updates

The common sense of choosing when new fixes are no longer essential may vary from very simple to very complex depending on your application. A short gap between when the location is obtained and when the location is used, enhances the accuracy of the approximation.

Constantly know that listening for a long time uses up a lot of battery power, so as soon as you have the information you require, you ought to stop listening for updates by

calling removeUpdates(PendingIntent):

// Remove the listener you previously added


Preserving a present best estimate

You may expect that the most recent location fix is the most accurate. Nevertheless, due to the fact that preciseness of a location fix differs, the most current fix is not always the best.

You ought apply common sense logic for selecting location fixes depending on a lot of criteria. The criteria as well differ depending on the use-cases of the application and field testing.

Here are a few steps you can take to authenticate the accuracy of a location fix:

Verify if the location retrieved is appreciably newer than the earlier estimate.

Check if the preciseness claimed by the location is better or worse than the previous calculation.

Check which provider the new location is from and find out if you trust it more.

An intricate example of this common sense can appear as shown below:

private static final int TWO_MINUTES = 1000 * 60 * 2;
/** Determines whether one Location reading is better than the current Location fix
* @param location The new Location that you want to evaluate
* @param currentBestLocation The current Location fix, to which you want to compare the new one

protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;

// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;

// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
return false;

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
return provider1.equals(provider2);

Add a Comment