Bring Location Services into Your Android App Development

One of the coolest features in Android app development is location services. After all, Android devices are mobile devices and mobile devices are, well, mobile. So knowing where the user is located can be really useful. For example, if you know where the user of your Android application is located, you can show them all of the garage sales in the vicinity, or where to get the cheapest gas price. Obviously, you would need to have access to that type of data, but you get the idea.

This Android development tutorial is a quick getting started guide for building location-aware Android apps. It explains how to enable your Android app to get a user's location and then do something with it. But before diving into the code, let's talk about Android location services for a bit.

Choosing Location Services for Your Android App

There are several ways to determine where an Android user is located. The two most prominent methods are to use the global positioning system (GPS) and Android's Network Location Provider.

GPS is the most accurate way to determine a user's location, but pinging a global positioning satellite too much will quickly drain a mobile device's battery and this method doesn't always work indoors.

Android's Network Location Provider figures out a user's location based on cell tower and Wi-Fi signals. It not only uses less battery power than GPS, but it's also faster and it works whether the user is outside or inside.

So, which one should you use in your Android app? Fortunately, you don't have to choose; you can use both services in your application. How you leverage these location providers will be dictated by the needs of your application.

Essentially, the choice between GPS and Android's Network Location Provider is largely weighing accuracy versus battery consumption. The example I present in this tutorial uses both GPS and the network provider. Download the full source code for the demo application.

Using GPS and Network Location Provider in Your Android App

Let's write some code. The application I present in this tutorial is really basic: It determines the location of the user and maps that location to a nearby street address, if possible.

First, you need to request permission to access the location feature of the Android device. To do that, add the following to your Android manifest file.

The ACCESS_FINE_LOCATION permission gives you access to both GPS and the Android Location Network Provider. If you need only the network provider, you can request the ACCESS_COARSE_LOCATION permission.

Next, set up your member variables and some constants.

public class MainActivity extends FragmentActivity {

// These are the three UI components we'll be updating.

private TextView mLatLng;

private TextView mAddress;

private TextView mLastUp;

private LocationManager mLocationMgr;

private Handler mHandler;

private Location mLastLocation;

private boolean mGeocoderAvailable;

private static final int UPDATE_LASTLATLNG = 4;

private static final int LAST_UP = 3;

private static final int UPDATE_LATLNG = 2;

private static final int UPDATE_ADDRESS = 1;

// These static variables control when to the application should check again for location.

The next method is setup. The setup method actually initiates the bulk of the work:

It fetches the user location from GPS.

If Step 1 fails, it attempts to get the location from the network provider.

If a location is determined, it calls the getBestLocation method to see if the new location is better than the old location.

The reason for Step 3 is that the new location might not be better than the old location. For example, if the previous location was pulled from a GPS satellite but the new location is pulled from the network provider, the old location might be more accurate. In that case, the application will stick with the old location for the time being.

Of course this application is of marginal value, but it does highlight how simple it is to access the location feature on Android devices.

When to Update the User's Location

Something to think about when building your location-aware Android application is determining when to stop listening for location updates. Listening for long periods of time drains a device's battery, which can be annoying to users.

However, at certain times, particularly if the user is able to plug the device into a power source, constant updating of the device's location is necessary. When doing that, I suggest running the real-time updating as a background process and using the system notification bar to make the user aware that it's happening.