Hi,

Chronometer as stopwatch is code as below.  But it seems some how
elapsedTime is 1 sec or so at times counted more than the reading on
chronometer.  I have tried to set the startTime, base time etc. to
favor calculating the correctly but no good.  any one can spot the
error.

logic:  allow start, stop, start, stop to compute total time.
on start I sync the base to elapsedRealtime so that when stopped I can
capture the time elapsed for the run.  On stop I allow to aggregate
the value and display it.  Reset code not shown clears the display
string and resets the total elapsed time.

        Button btn=(Button)findViewById(R.id.startTimer);

        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

               chrono.setBase(SystemClock.elapsedRealtime());
               chrono.start();
               startTime = SystemClock.elapsedRealtime();

            }
        });

        btn=(Button)findViewById(R.id.stopTimer);

        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                long elapsedTime = SystemClock.elapsedRealtime() -
chrono.getBase();
                chrono.stop();



                totalElapsedTime += elapsedTime;
                long minutes = (totalElapsedTime / 1000 ) / 60;
                long seconds = (totalElapsedTime / 1000 ) % 60;
                displayTotalTime = "" + minutes + ":" + seconds;

                updateLabel();
            }
        });

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to