Take advantage of Android's GPS API

Android app developer William J. Francis walks you through the most basic of GPS usage scenarios in this tutorial.

Modern smartphones have a host of sensors on them; there are accelerometers, cameras, microphones, and the global positioning system (GPS). I'm not sure how I ever got anywhere before Google Maps.

It's not difficult to use Android's GPS API in your own applications. There are, however, a number of steps you must follow and a few guidelines to keep in mind. This tutorial will walk you through the most basic of GPS usage scenarios. You can follow along with the detailed instructions below or download and import the entire project into Eclipse.

1. Create a new Android project in Eclipse. Target Android 1.6 or higher. Be sure to rename the startup activity to and the corresponding layout to main.xml.

2. Android provides several mechanisms for getting a user's location; these mechanisms include network, passive, and GPS. You can find a description of all three in the official Android documentation. For the purposes of this tutorial, we will only be using the GPS mechanism. Therefore, in the AndroidManifest.xml file, we will need to add a single permission for ACCESS_FINE_LOCATION.

<manifest xmlns:android=""
    android:versionName="1.0" >
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        android:theme="@style/AppTheme" >
            android:label="@string/title_activity_main" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

3. The layout for our demonstration is defined in /res/layout/main.xml. It is a simple linear layout with two labels and a button stacked vertically.


<LinearLayout xmlns:android=""
        android:text="Quick GPS Demo"/>
        android:text="Fine Location = (waiting on gps...)"
        android:id="@+id/locat_label" />
        android:text="GPS Settings"

4. The /src file extends Activity while implementing both an OnClickListener and a LocationListener. We use the standard Android OnCreate override to wire up both the button and the location manager. Note the constant MIN_TIME in our class; this is how frequently the GPS is updated. You need to keep this at a reasonable value or you will drain the phone battery very quickly. For the purpose of the demo I used 60 seconds, but even that might be excessive in a live application.

package com.authorwjf.quickgpsdemo;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class Main extends Activity implements OnClickListener, LocationListener {
       private LocationManager locationManager;
       private static final long MIN_TIME = 1 * 60 * 1000; //1 minute
    public void onCreate(Bundle savedInstanceState) {
       locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
       locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME, 0, this);

5. The on click listener is responsible for invoking the system menu to allow a user to enable or disable the GPS. This is needed because for security reasons an application cannot turn on the GPS without user interaction. If that sounds like a pain it is meant to be. While as a developer the idea of being able to toggle the GPS hardware on and off at will sounds nice, I am also an Android user, and I sure don't want a rogue app running down my battery or worse uploading my location to a website for thieves alerting the world every time I leave my house.

public void onClick(View v) {
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);

6. Next we will be responding to the location updates. To keep things simple, we are ignoring the callbacks for location services enabled/disabled; however, you must include them, and the usefulness of maintaining state should be apparent.

public void onLocationChanged(Location loc) {
int lat = (int) (loc.getLatitude());
int lng = (int) (loc.getLongitude());
         ("Fine Location = (lat:"+Integer.toString(lat)+",
public void onProviderDisabled(String arg0) {
       // TODO Auto-generated method stub
public void onProviderEnabled(String arg0) {
       // TODO Auto-generated method stub
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
       // TODO Auto-generated method stub

7. When dealing with hardware especially, it is always a good idea to handle the pause and resume states of the Android lifecycle.

protected void onResume() {
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, 0, this);
protected void onPause() {
You can load the application to your device and give it a try (Figure A). Remember you will need to use the button to enable the GPS, and then wait at least 60 seconds for the update to occur. Figure A

If you are running the demo on an emulator, you will have to open a command prompt and telnet into the AVD to fake a location update.

First type:

telnet localhost 5554

and then:

geo fix 25 20

There, that wasn't so bad, was it? Just remember the two golden rules: only a user can enable the GPS, and keep your location access to a minimum to preserve the battery.

About William J. Francis

William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intellige...

Editor's Picks