3

I am experimenting with using a stopwatch in one of my apps and I have got it to where it will start counting seconds on start and stop counting on stop. My problem is that it will keep going on after 60 seconds. For example I get 120 seconds if I waited for two minutes.

So my question is how can I make it so once the seconds reached 60 the minutes would be increased by one and the seconds would start over?

So instead of :120 I would get 2:00. Here is the code I have:

final int MSG_START_TIMER = 0;
final int MSG_STOP_TIMER = 1;
final int MSG_UPDATE_TIMER = 2;

Stopwatch timer = new Stopwatch();
final int REFRESH_RATE = 100;

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

        case MSG_UPDATE_TIMER:              
            tvTextView.setText(":"+ timer.getElapsedTimeSecs());                
            mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER,REFRESH_RATE); 
            break;                                  
        case MSG_STOP_TIMER:
            mHandler.removeMessages(MSG_UPDATE_TIMER); 
            timer.stop();
            tvTextView.setText("");
            break;

        default:
            break;
        }
    }
};

Also are:

 public void start(View v) {
    mHandler.sendEmptyMessage(MSG_START_TIMER);
}

public void stop(View v) {
    mHandler.sendEmptyMessage(MSG_STOP_TIMER);
}

By the way I looked at this question but his issue was with TimeSpan and if I understand correctly that is different from Stopwatch(correct me if I am wrong). Thanks for your time and effort.

Community
  • 1
  • 1
ninge
  • 1,592
  • 1
  • 20
  • 40
  • That question you referred to is C#! – t0mm13b Aug 12 '12 at 00:31
  • Look at this question - http://stackoverflow.com/questions/5937043/problem-with-stopwatch-in-android?rq=1 – t0mm13b Aug 12 '12 at 00:32
  • Why did you vote my question down? Just want to know so I can improve future questions. – ninge Aug 12 '12 at 00:41
  • Sure, you should have checked around first, when you click on ask a question, and put in a title, there's a dynamic list underneath the title, where similar questions were asked. Also, you referred to something that was completely different. That's a problem with SO, where users actually do not check any similar sounding questions first before posting; the end result is a lot of potential duplicates will arise and cause headaches for those involved as in "Should we remove the question by marking as duplicate" etc and voting down as a result – t0mm13b Aug 12 '12 at 00:44
  • Thanks for the feedback I didn't see any similar questions in that dynamic list underneath the title and I didn't see anything on Google either. Also the question you provided had a different problem: he already knew how to format min and sec. Here just wanted to start with 00:00:00 rather than 0:0:0. Thanks again for explaining yourself. – ninge Aug 12 '12 at 00:47
  • Its on the right hand side, if you did not see a similar question, its because the title of your question did not get a hit on it, but if you look at the right hand side -> there's similar questions pertaining to the keyword _stopwatch_ :) Let me slightly adjust the formatting of the question - to show my generousity in reverting the downvote :) – t0mm13b Aug 12 '12 at 00:50
  • There, slightly re-formatted and reverted the downvote to show my generousity ;) – t0mm13b Aug 12 '12 at 00:52

2 Answers2

8

If you start with the time in seconds:

display = String.format("%d:%02d", seconds / 60, seconds % 60);

If you don't want the minutes if they are 0:

if (seconds < 60)
    display = String.format("%02d", seconds);
else
    display = String.format("%d:%02d", seconds / 60, seconds % 60);
MRAB
  • 20,356
  • 6
  • 40
  • 33
2

You are just going to have to make a function that creates a string for you. Stash this somewhere reachable.

    public String NumToStr(long i){
        if (i < 10 ) {
            return ("0" + Long.toString(i));
        }
        return Long.toString(i);
    }

this will take any number that is less than 10 and give you a string with a "0" infront of the number.

i.e. send it a 7 and get "07" then send it a 55 and get "55" because it was not less than 10. Finaly send it the seconds 5 and get "05". Now add the strings together,

    Hour + ":" + Minute + ":" + Seconds;

and you will get "07:55:05"

next just put in a

    if (seconds > 60) {
        seconds = 0
        minutes++; 
    }
    if (minutes > 60) {
        minutes = 0
        hours++;
    }
    if (hours > 12) {
        hours = 0
    }

With all respect this is basic stuff.

In response to where should you put all of this? Where ever you like. But you need to redo some of your code.

     case MSG_UPDATE_TIMER:
        long TimePassed;
        TimePassed = Seconds;
        if (Minutes > 0) {
            TimePassed = TimePassed  + (60 * Minutes);
        }
        if (Hours > 0) {
            TimePassed = TimePassed + (60 * 60 * Hours);
        }

        Seconds =  (timer.getElapsedTimeSecs()- TimePassed );

        if (Seconds > 60) {
            Seconds = 0
            Minutes++; 
        }
        if (Minutes > 60) {
            Minutes = 0
            hours++;
        }
        String timeSecs = NumToStr(Seconds);
        String timeMins = NumToStr(Minutes);
        String timeHours = NumToStr(Hours);

        String Time = timeHours + ":" + timeMins + ":" + timeSecs; 

        tvTextView.setText(Time);   

        mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER,REFRESH_RATE); 
        break;  
WIllJBD
  • 6,144
  • 3
  • 34
  • 44
  • Where should I put all of this? – ninge Aug 12 '12 at 00:40
  • Dont forget you need to declare Seconds, Minutes, and Hours as Longs in your code up by your other 3 final int's. – WIllJBD Aug 12 '12 at 00:59
  • I am sure your answer would have done the trick also, but I accepted MARB's answer because of the simplicity. But +1 for you because of the great explaining of your answer. – ninge Aug 12 '12 at 01:01
  • I just wanted something you could understand how it worked. But it's all good. – WIllJBD Aug 12 '12 at 01:02