×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    11

    Location based service with notchy stopwatch

    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:

    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 stopwatch thread:

    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) {
                
                }
            }
        }
    }
    The positioningrunner:

    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();;
            }
        } 
    }
    And the positionlistener implementation:



    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();
                }
            }
        }   
    }

  2. #2
    Regular Contributor
    Join Date
    Mar 2008
    Posts
    198

    Re: Location based service with notchy stopwatch

    Quote Originally Posted by nicsoft View Post
    I am developing some kind of LBS/GPS service which should measure distance but also elapsed time.
    Hi Niklas,

    You're trying to use your phone as a precision time measurement device ... it's not going to work

    Java Micro Edition simply isn't designed to work with this level of timing precision, and to do it properly you would need a real-time operating system, which you definitely don't get on any phone.

    It would be minor miracle if you can get this to work in 1/10ths of a second... and what would you do if the user gets a call when the application is running?

    - Mike
    NAVTEQ Network for Developers
    The community for developing innovative location-based applications
    http://NN4D.com

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    11

    Re: Location based service with notchy stopwatch

    Thanks for your reply!

    You may be right, and thinking of it, it really doesn't matter that precision. I just got stuck on the though (kind of "00:00:00:0" looks uggly so I need a better precision to get "00" at the end, but it's better removing one, then it still look nice and tidy). Thank you for helping me break my thought! I will go for a precision of 1s from now on. That will work fine most certainly.

    Regards,
    Niklas

Similar Threads

  1. Location Based Service
    By navimoto in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2008-03-31, 08:57
  2. getting country - location based service
    By funworld_0815 in forum Mobile Java General
    Replies: 4
    Last Post: 2003-08-18, 00:25

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×