2

Basically, I have a count down timer right? And I need to display the time left for the timer in text. I have done that, but the timer's time is a bit off, as it needs to convert the seconds left into hours and minutes, like on a regular digital watch where it says 00:05:00 and it counts down to 00:04:59. I've done a lot of things today and my head is hurting quite a bit at the moment, so I can't exactly think about it. So my guess is that I need to use multiples of 60. Help?

Code:

int timeinminutes=1;

    new CountDownTimer(timeinminutes*100000, 1000) {

        TextView mTextField = (TextView) findViewById(R.id.textView1);

         public void onTick(long millisUntilFinished) {
             long hrs=0;
             long mnts=0;
             long scnds=0;
             scnds=(millisUntilFinished/1000);
             if (scnds>59) {
                 mnts=(scnds/60);
                 if (mnts!=Math.floor(mnts)) {
                     mnts=0;
                 }

             }
             if (mnts>59) {
                 hrs=(mnts/60);
                 if (hrs!=Math.floor(hrs)) {
                     hrs=0;
                 }
             }
             mTextField.setText(hrs + ":" + mnts + ":" + scnds);
         }

         public void onFinish() {
             mTextField.setText("00:00:00");
         }
      }.start();

2 Answers2

0

A lot of this has already been done. If you read about SimpleDateFormat it should help you get on your way to solving this.

You can find more information here as well.

Check out java.util.Timer here.

These should solve any of your problems.

Community
  • 1
  • 1
Kyle
  • 893
  • 1
  • 9
  • 20
0

You should have a separate class that does the count-down and keeps the remaining time in milliseconds as a field. The class could have a method that launches a Thread which sleeps 1000 milliseconds, then subtracts 1000 from the original time in milliseconds until the remaining time is 0.

You then could use another Thread which updates the TextView every 1000 milliseconds and a SimpleDateFormat to get the remaining time from the countdowntimer object and format it.

Demonick
  • 2,116
  • 3
  • 30
  • 40