Hello,
I am developing some kind of LBS/GPS service which should measure distance but also elapsed time.
Everything works pretty fine except the appearence of the presented elapsed time. I would like to make the updates very regular for each 1/100 of a second but now it is very irregular and, hence, looks uggly/notchy. The actual time should be correct though.
The stopwatch implementation works well and looks good when not using it in combination with the location based functionality.
Is there any way to make the presentation of the elapsed time looks nice and preferable be updated every 1/100 of a second, or at least with regular intervalls together with location based functionality?
I have tried using a normal Timer functionality, didn't look better. I am getting a bit worried that perhaps there is no good way of doing it...
Please see the code below!
Thank you in advance!
Regards,
Niklas
My code (I did remove a lot in order to make the code cleaner. It's not possible to execute it like this...)
The MIDlet:
The stopwatch thread:Code:public class WLListenerMIDlet extends MIDlet implements CommandListener { private PositioningRunner pRunner = null; private Thread pThread = null; private StopWatchThread thread = null; private String navigateFrom = ""; public WLListenerMIDlet(){ } public void startApp() { pRunner = new PositioningRunner(this); pThread = new Thread(pRunner); pThread.start(); thread=new StopWatchThread(elapsedTime); thread.start(); } public void pauseApp() { } public void writeInfo(String msg){ } public void writeDistance(String msg){ } public void writeStatus(String msg){ } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s){ }
The positioningrunner:Code:import java.util.*; import javax.microedition.lcdui.*; public class StopWatchThread extends Thread { boolean paused=true; boolean stopped=false; private Date startTime, pauseTime; private long totaltime=0; private StringItem elapsedTime = null; public StopWatchThread(StringItem elapsedTime) { this.elapsedTime=elapsedTime; } //Starts the stopwatch (or resumes after being paused) public synchronized void startWatch() { stopped=false; if(startTime==null) startTime=new Date(); paused=false; //Wake up the thread notifyAll(); } private String getTime() { Calendar c=Calendar.getInstance(); Date rightNow=new Date(); c.setTime(new Date(rightNow.getTime()-startTime.getTime()+totaltime)); //We work with minutes, seconds and centiseconds int hour=c.get(c.HOUR) - 1; int min=c.get(c.MINUTE); int sec=c.get(c.SECOND); int csec=c.get(c.MILLISECOND)/10; String shour; String smin; String ssec; String scsec; //Format the values so that they will look appropriate when displayed if(hour<10) shour="0"+hour; else shour=String.valueOf(hour); if(min<10) smin="0"+min; else smin=String.valueOf(min); if(sec<10) ssec="0"+sec; else ssec=String.valueOf(sec); if(csec<10) scsec="0"+csec; else scsec=String.valueOf(csec); return shour+":"+smin+":"+ssec+":"+scsec; } public void run() { while(threadIsRunning()) { try { if(!isPaused()) { try { elapsedTime.setText(getTime()); } catch(Exception e) { } Thread.sleep(10); } else { synchronized(this) { wait(); } } } catch(InterruptedException e) { } } } }
And the positionlistener implementation:Code:import javax.microedition.lcdui.StringItem; import javax.microedition.location.LocationProvider; public class PositioningRunner implements Runnable { private StringItem err = null; private StringItem status = null; private LocationProvider lp = null; private PositioningListener pListener = null; private boolean running; private boolean stopped; private WLListenerMIDlet wl = null; public PositioningRunner (WLListenerMIDlet wl) { this.wl = wl; } public void run(){ try{ lp = LocationProviderAdapter.getLocationprovider(); pListener = new PositioningListener(wl); lp.setLocationListener(pListener, 1, -1, -1); while(isRunning()){ if(!stopped){ } } } catch(Exception ex){ err.setText(ex.getMessage()); System.out.println(ex.getMessage() + "-> " ); ex.printStackTrace();; } } }
Code:import javax.microedition.location.Coordinates; import javax.microedition.location.Location; import javax.microedition.location.LocationListener; import javax.microedition.location.LocationProvider; public class PositioningListener implements LocationListener{ private WLListenerMIDlet wl = null; private Coordinates co = null; private Coordinates oldCo = null; private float totMeter = 0; private static final Object LOCATION_LOCK = new Object(); private static final Object STATE_LOCK = new Object(); public PositioningListener () { } public PositioningListener (WLListenerMIDlet wl) { this.wl = wl; } public void locationUpdated(LocationProvider provider, Location location){ synchronized(LOCATION_LOCK){ try{ if(location.isValid()){ co = location.getQualifiedCoordinates(); if(co!=null){ if(oldCo!=null){ totMeter += co.distance(oldCo); } oldCo = co; } } wl.writeDistance(Float.toString(totMeter)); wl.writeInfo(Float.toString(location.getQualifiedCoordinates().getAltitude())); LOCATION_LOCK.notifyAll(); }catch(Exception e){ System.out.println("locationUpdated: " + " " + e.getMessage()); e.printStackTrace(); } } } }

Reply With Quote
... it's not going to work 



