Add the following line to your Android Manifest, above the application tag:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Then you'll need an Activity or a Service that has code like this:
LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
onLocationChanged(Location location) {
// put code in here to respond to a change in location
}
onProviderDisabled(String provider) {
// put code in here to respond to GPS being disabled
}
onProviderEnabled(String provider) {
// put code in here to respond to GPS being enabled
}
onStatusChanged(String provider, int status, Bundle extras) {
// put code in here to respond to change in GPS status (e.g. GPS becomes unavailable)
}
});
Edit: When you call LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener()...
this sets a new LocationListener that listens to the GPS. The first number indicates the minimum number of milliseconds between each check (note this is just an indicator, the system may check more/less frequently), and the second number indicates the minimum distance travelled in metres between each check. So if both are 0, then the GPS will be checked constantly, and new values will be sent to onLocationChanged()
as soon as they are available.
For more detailed information, see http://developer.android.com/guide/topics/location/obtaining-user-location.html