package se.nekotronic.gpstest3; import java.util.Timer; import java.util.TimerTask; 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 GPSTest3Activity extends Activity { private TextView texten = null; private LocationManager locationManager = null; private int starts = 0; private Timer theTicker = null; private TimerTask theTimerTask = null; private String waitingText = null; 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(); if (waitingText != null) { String toPrint = waitingText; waitingText = null; print(toPrint); } } /** 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); } // theTicker = new Timer(); theTimerTask = new TimerTask() { @Override public void run() { try { Runnable r = new Runnable() { @Override public void run() { print("Tick!"); } }; texten.getHandler().post(r); // print("Tick!"); -- ger CalledFromWrongThreadException // texten.setText("X!"); -- ger CalledFromWrongThreadException } catch (Exception e) { waitingText = "run-print failed: " + e; } } }; // theTicker.schedule(theTimerTask, 3000, 1000); } // 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); } @Override protected void onPause() { print("onPause"); theTicker.cancel(); theTicker = null; super.onPause(); } @Override protected void onRestart() { print("onRestart"); super.onRestart(); } @Override protected void onResume() { print("onResume"); try { theTicker = new Timer(); theTimerTask = new TimerTask() { @Override public void run() { try { Runnable r = new Runnable() { @Override public void run() { print("Tick!"); } }; texten.getHandler().post(r); // print("Tick!"); -- ger CalledFromWrongThreadException // texten.setText("X!"); -- ger CalledFromWrongThreadException } catch (Exception e) { waitingText = "run-print failed: " + e; } } }; theTicker.schedule(theTimerTask, 3000, 1000); } catch (Exception e) { print("Resume failed: e = " + e); } super.onResume(); } @Override protected void onStart() { print("onStart"); super.onStart(); } @Override protected void onStop() { print("onStop"); super.onStop(); } }