0

I'm making stopwatch application for Android. I want when stop is clicked the time to stop, and when the Start is clicked again the time to be resumed. I'm using http://www.goldb.org/stopwatchjava.html class. Currently the time is resumed on click on Start button, but when i click Stop after second time it's showing bad time. Again on Start button the time is resumed correctly. Here's my code:

Handler mHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
        case MSG_START_TIMER:
            timer.start(); //start timer
            mHandler.sendEmptyMessage(MSG_UPDATE_TIMER);
            break;

        case MSG_UPDATE_TIMER:

            time = timer.getElapsedTime() + timeStoped;

            hh = time / 3600000;
            hours.setText("" + formatter.format(hh));
            time = time - hh * 3600000;

            mm = time / 60000;
            minutes.setText("" + formatter.format(mm));
            time = time - mm * 60000;

            ss = time / 1000;
            seconds.setText("" + formatter.format(ss));
            time = time - ss * 1000;

            millis.setText("" + formatter.format(time / 10));

            mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER,REFRESH_RATE); //text view is updated every second, 
            break;      
                                                                             //though the timer is still running
        case MSG_STOP_TIMER:

            mHandler.removeMessages(MSG_UPDATE_TIMER); // no more updates.
            timer.stop();//stop timer

            time = timer.getElapsedTime();
            timeStoped = timeStoped + time;

            hh = time / 3600000;
            hours.setText("" + formatter.format(hh));
            time = time - hh * 3600000;

            mm = time / 60000;
            minutes.setText("" + formatter.format(mm));
            time = time - mm * 60000;

            ss = time / 1000;
            seconds.setText("" + formatter.format(ss));
            time = time - ss * 1000;

            millis.setText("" + formatter.format(time / 10));

            break;

        default:
            break;
        }
    }
};

the Listener for the button Start/Stop :

startBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (!startIsPressed) {
                mHandler.sendEmptyMessage(MSG_START_TIMER);
                startIsPressed = true;
                startBtn.setText(R.string.stop);
            } else {
                startIsPressed = false;
                startBtn.setText(R.string.start);
                mHandler.sendEmptyMessage(MSG_STOP_TIMER);
            }
        }
    });
dgjorg
  • 85
  • 8
  • you have add Stopwatch class from http://www.goldb.org/stopwatchjava.html in your project? – ρяσѕρєя K May 12 '12 at 16:08
  • yes, i have added. The problem is that timer.start(); is called every time when start is pressed (so on Stop the bad time is shown), but I don't have clue how to solve it – dgjorg May 12 '12 at 16:10
  • can you explain more about your problem.bez i'm not getting you? – ρяσѕρєя K May 12 '12 at 16:17
  • Scenario: 1. Click Start button start counting 2. Click Stop button we have 00:00:05.35 3. Click again on Start it continues from 00:00:05.35 4. Click Stop shows bad time (time measured from 3. to 4.) 5. Click Start it is resuming correctly On every Stop (except for the first one) bad time is shown – dgjorg May 12 '12 at 16:20
  • you have try after replacing timeStoped = timeStoped + time; hh = time / 3600000; hours.setText("" + formatter.format(hh)); time = time - hh * 3600000; mm = time / 60000; minutes.setText("" + formatter.format(mm)); time = time - mm * 60000; ss = time / 1000; seconds.setText("" + formatter.format(ss)); time = time - ss * 1000; with millis.setText("" + timer.getElapsedTime()); – ρяσѕρєя K May 12 '12 at 16:24

2 Answers2

2

The solution is to replace:

    time = timer.getElapsedTime();
    timeStoped = timeStoped + time;

with:

    time = timer.getElapsedTime() + timeStopped;
    timeStopped = time;
dgjorg
  • 85
  • 8
0

An easier way to set the time in hour:minute:second format might be:

Format formatter = new SimpleDateFormat("hh mm ss SSS");
String[] parsedTime = formatter.format(time).split(" ");
hours.setText(parsedTime[0]);
minutes.setText(parsedTime[1]);
seconds.setText(parsedTime[2]);
millis.setText(parsedTime[3]);

To keep accurate time after multiple stop your need to change this:

        time = timer.getElapsedTime();
        timeStoped = timeStoped + time;

To something that checks for a previous elapsed time as well, maybe like this:

        time = timer.getElapsedTime() + time;
        timeStoped = timeStoped + time;

And these are the same as:

        time += timer.getElapsedTime();
        timeStoped += time;
Sam
  • 86,580
  • 20
  • 181
  • 179