package se.nekotronic.gpstest4; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; 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 GPSTest4Activity extends Activity { private TextView texten = null; private LocationManager locationManager = null; private Location latestLocation = null; private Time latestLocationTime = null; private Time startTime = null; private int starts = 0; private int locations = 0; private Timer theTicker = null; private TimerTask theTimerTask = null; private String waitingText = null; private boolean providerEnabled = false; private boolean providerEnabledHasChanged = false; private Time providerEnabledChangeTime = null; private LocationListener locationListener; private void print(String text) { waitingText = 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(); if (waitingText != null) { String toPrint = waitingText; waitingText = null; print(toPrint); } */ } public static final double roundDouble(double d, int places) { return Math.round(d * Math.pow(10, (double) places)) / Math.pow(10, (double) places); } private void updateText() { Time now = new Time(); now.setToNow(); String text = "Time " + now.format("%H:%M:%S") + "\n"; text += "(Start " + startTime.format("%H:%M:%S") + ")\n"; text += providerEnabledHasChanged ? providerEnabled ? "GPS On" : "GPS Off" : "GPS unknown status"; if (providerEnabledChangeTime != null) text += " since " + providerEnabledChangeTime.format("%H:%M:%S"); text += "\n"; if (latestLocation == null) { text += "No location yet\n"; } else { double longitude = latestLocation.getLongitude(); double latitude = latestLocation.getLatitude(); double accuracy = latestLocation.getAccuracy(); double altitude= latestLocation.getAltitude(); text += "" + locations + ": " + "Lat " + Location.convert(latitude, Location.FORMAT_SECONDS) + " " + "Long " + Location.convert(longitude, Location.FORMAT_SECONDS) + "\n" + "Accuracy " + accuracy + " " + "Altitude " + altitude + "\n"; } if (latestLocationTime != null) { text += "Retrieved " + latestLocationTime.format("%H:%M:%S"); long seconds = (now.toMillis(false) - latestLocationTime.toMillis(false)) / 1000; text += " (Age " + seconds + ")\n"; } if (waitingText != null) text += "Message: " + waitingText; texten.setText(text); texten.invalidate(); // texten.postInvalidate(); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // texten = (TextView)findViewById(R.id.textview); texten = new TextView(this); texten.setTextSize(30.0f); texten.setText(""); ++starts; // print("onCreate (" + starts + ")"); setContentView(texten); startTime = new Time(); startTime.setToNow(); locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); // Define a listener that responds to location updates 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) { providerEnabled = true; providerEnabledHasChanged = true; providerEnabledChangeTime = new Time(); providerEnabledChangeTime.setToNow(); // print("onProviderEnabled"); } public void onProviderDisabled(String provider) { providerEnabled = false; providerEnabledHasChanged = true; providerEnabledChangeTime = new Time(); providerEnabledChangeTime.setToNow(); // 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); } */ // theTicker = new Timer(); theTimerTask = new TimerTask() { @Override public void run() { try { Runnable r = new Runnable() { @Override public void run() { // print("Tick!"); updateText(); } }; texten.getHandler().post(r); // print("Tick!"); -- ger CalledFromWrongThreadException // texten.setText("X!"); -- ger CalledFromWrongThreadException } catch (Exception e) { waitingText = "run-print failed: " + e; } } }; // theTicker.schedule(theTimerTask, 1000, 1000); } // onCreate private void makeUseOfNewLocation(Location location) { ++locations; latestLocation = location; latestLocationTime = new Time(); latestLocationTime.setToNow(); updateText(); /* 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); */ } @Override protected void onPause() { // print("onPause"); try { // locationManager.removeUpdates(locationListener); // Register the listener with the Location Manager to receive location updates locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 24*60*60*1000, 0, locationListener); } catch (Exception e) { print("requestLocationUpdates failed: " + e); } theTicker.cancel(); theTicker = null; super.onPause(); } @Override protected void onRestart() { // print("onRestart"); super.onRestart(); } @Override protected void onResume() { // print("onResume"); super.onResume(); if (this.latestLocation == null) { Location l = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (l != null) makeUseOfNewLocation(l); else { l = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (l != null) makeUseOfNewLocation(l); } } 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); } try { theTicker = new Timer(); theTimerTask = new TimerTask() { @Override public void run() { try { Runnable r = new Runnable() { @Override public void run() { // print("Tick!"); updateText(); } }; texten.getHandler().post(r); // print("Tick!"); -- ger CalledFromWrongThreadException // texten.setText("X!"); -- ger CalledFromWrongThreadException } catch (Exception e) { waitingText = "run-print failed: " + e; } } }; theTicker.schedule(theTimerTask, 1000, 1000); } catch (Exception e) { print("Resume failed: e = " + e); } updateText(); } // onResume @Override protected void onStart() { // print("onStart"); super.onStart(); } @Override protected void onStop() { // print("onStop"); super.onStop(); } }