package se.nekotronic.gpstest2; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.text.format.Time; import android.widget.TextView; public class GPSTest2Activity extends Activity { private TextView texten = null; private LocationManager locationManager = null; private int starts = 0; private void print(String text) { Time now = new Time(); now.setToNow(); String timeString = now.format("%H:%M:%S"); texten.setText(timeString + ": " + text + "\n" + texten.getText()); texten.invalidate(); texten.postInvalidate(); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); texten = new TextView(this); texten.setText(""); ++starts; print("onCreate (" + starts + ")"); setContentView(texten); locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); // Define a listener that responds to location updates LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // Called when a new location is found by the network location provider. print("onLocationChanged"); makeUseOfNewLocation(location); } public void onStatusChanged(String provider, int status, Bundle extras) { print("onStatusChanged"); } public void onProviderEnabled(String provider) { print("onProviderEnabled"); } public void onProviderDisabled(String provider) { print("onProviderDisabled"); } }; try { // Register the listener with the Location Manager to receive location updates locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } catch (Exception e) { print("requestLocationUpdates failed: " + e); } } // onCreate private void makeUseOfNewLocation(Location location) { double longitude = location.getLongitude(); double latitude = location.getLatitude(); double accuracy = location.getAccuracy(); double altitude= location.getAltitude(); print("Long " + longitude + ", lat " + latitude + ", accuracy " + accuracy + ", alt " + altitude); } }