public void setLocationListener( LocationListener listener, int interval, int timeout, int maxAge) { // * Stop all previous listener threads * listenerRunning = false; if (listyThread != null) { while (listyThread.isAlive()) { Thread.yield(); } // End old thread listyThread = null; // Discard the listener thread instance } // * Remove any listeners from GPSListener * if (listener == null) { // Remove current listener from SimpleGPS SimpleGPS.removeListener(gpsl); gpsl = null; return; // No listener provided, so return now so it dosn't make a new one } // * Inner classes need final variables * final int to = timeout; final LocationListener l = listener; final LocationProvider lp = this; final int delay = interval * 1000; // Oddly interval is in seconds, and not float // Make new thread here and start it if interval > 0, else if -1 // then use the GPSListener interface. if (interval > 0) { // Notify according to interval by user listyThread = new Thread() { public void run() { while (listenerRunning) { try { // TODO: Probably only notify if location changed? Need to compare to old. // TODO: Make helper method since this is used below too. l.locationUpdated(lp, lp.getLocation(to)); Thread.sleep(delay); } catch (LocationException e) { // TODO Auto-generated catch block } catch (InterruptedException e) { // TODO Auto-generated catch block } } } }; listyThread.setDaemon(true); // so JVM exits if thread is still running listenerRunning = true; listyThread.start(); } else if (interval < 0) { // If interval is -1, use default update interval // In our case, update as soon as new coordinates are available from GPS (via GPSListener) // TODO: Alternate method: Use GPSListener for ProximityListener and this. gpsl = new GPSListener() { public void sentenceReceived(NMEASentence sen) { // Check if GGASentence. Means that new location info is ready if (sen.getHeader().equals(GGASentence.HEADER)) { try { // TODO: Probably only notify if location changed? Need to compare to old. l.locationUpdated(lp, lp.getLocation(to)); } catch (LocationException e) { // TODO Auto-generated catch block } catch (InterruptedException e) { // TODO Auto-generated catch block } } } }; SimpleGPS.addListener(gpsl); } // TODO: Need to implement LocationListener.providerStateChanged() }