How to:‎ > ‎

Using the GPS

Using the GPS

Important information about accessing the GPS can be found on the Developer web page. I recommend that you look up “LocationManager” and “LocationListener”.

The class description for LocationManager is:

This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.

The description of the LocationListener interface is:

Used for receiving notifications from the LocationManager when the location has changed. These methods are called if the LocationListener has been registered with the location manager service using the requestLocationUpdates(String, long, float, LocationListener) method.

GPS requires permissions to be placed in the AndroidManifest.xml file:







<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android=""




    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".GPSTest"



                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />





android:name="android.permission.ACCESS_COARSE_LOCATION" />


android:name="android.permission.ACCESS_FINE_LOCATION" />


android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />


android:name="android.permission.ACCESS_MOCK_LOCATION" />


android:name="android.permission.CONTROL_LOCATION_UPDATES" />

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


Example code for accessing GPS

About the code example: GPS is really slow to load. It may take 6 to 8 minutes, especially if coverage is poor. Sending output to Log will not print to screen. If you want GPS data to show up on the screen you have to create a TextView object and send output there. This example prints latitude and longitude to the screen, only after the GPS has actually determined its position. During the first few minutes you won't see anything, since no updates have occurred.

The GPS Activity needs to implement LocationListener. You also need a LocationManager object.

You need to call the method getSystemService(Context) and give it Context.LOCATION_SERVICE

Calls to the LocationManager will give information about where you are. The requestLocationUpdates() method will retrieve information from the GPS.


package com.example;


import android.content.Context;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.util.Log;

import android.widget.TextView;

public class GPSTest extends Activity implements LocationListener {

private LocationManager lm;

private TextView tv;

    /** Called when the activity is first created. */


    public void onCreate(Bundle savedInstanceState) {



        tv = (TextView) findViewById(;

        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);



    public void onLocationChanged(Location arg0) {

        String lat = String.valueOf(arg0.getLatitude());

        String lon = String.valueOf(arg0.getLongitude());

        Log.e("GPS", "location changed: lat="+lat+", lon="+lon);

        tv.setText("lat="+lat+", lon="+lon);


    public void onProviderDisabled(String arg0) {

        Log.e("GPS", "provider disabled " + arg0);


    public void onProviderEnabled(String arg0) {

        Log.e("GPS", "provider enabled " + arg0);


    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

        Log.e("GPS", "status changed to " + arg0 + " [" + arg1 + "]");



Note:  This code was an example found on the Internet.  It was not written by me, however I have tested it and it gives GPS location data on my Droid phone.