package se.nekotronic.simplegps; import java.text.DecimalFormat; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.location.LocationProvider; import android.os.Bundle; import android.text.format.Time; import android.widget.TextView; public class SimpleGPSTestActivity extends Activity { private TextView texten = null; private LocationManager location_manager; private LocationListener location_listener; private void print(String text) { Time now = new Time(); now.setToNow(); String timeString = now.format("%H:%M:%S"); String line = timeString + ": " + text + "\n"; texten.setText(texten.getText() + line); texten.invalidate(); texten.postInvalidate(); } DecimalFormat dec = new DecimalFormat("0.0000"); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); texten = new TextView(this); texten.setText(""); setContentView(texten); print("onCreate"); location_manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); location_listener = new LocationListener() { @Override public void onLocationChanged(Location location) { double longitude = location.getLongitude(); double latitude = location.getLatitude(); double accuracy = location.getAccuracy(); double altitude= location.getAltitude(); print("New location: Long " + dec.format(longitude) + ", lat " + dec.format(latitude) + ", accuracy " + dec.format(accuracy) + ", alt " + dec.format(altitude)); } @Override public void onProviderDisabled(String provider) { print("Location provider has been disabled."); } @Override public void onProviderEnabled(String provider) { print("Good news! Location provider has been enabled."); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { if (status == LocationProvider.OUT_OF_SERVICE) print("GPS status changed to OUT_OF_SERVICE."); else if (status == LocationProvider.TEMPORARILY_UNAVAILABLE) print("GPS status changed to TEMPORARILY_UNAVAILABLE."); else if (status == LocationProvider.AVAILABLE) print("Good news! GPS status changed to AVAILABLE."); } }; } @Override protected void onPause() { super.onPause(); print("onPause"); location_manager.removeUpdates(location_listener); } @Override protected void onResume() { super.onResume(); print("onResume"); try { // Register the listener with the Location Manager to receive // location updates location_manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, location_listener); } catch (Exception e) { print("Couldn't use the GPS: " + e + ", " + e.getMessage()); } } }