Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Displaying Seconds Ticker

Tags:

android

I need to display a timer with hours, minutes and seconds. Currently I use android.os.CountDownTimer(1000*60*60, 1000). I have a feeling it's not a perfect fit for my needs, because I don't need it to stop, so I resort to entering huge values as the countdown value.

Am I perhaps missing another method for showing time ticks, indefinitely?

like image 672
Hein du Plessis Avatar asked Jul 17 '26 14:07

Hein du Plessis


1 Answers

In my case the example above start working only after i made changes like this:

   private final Runnable mUpdateTimeTask = new Runnable() {
        @Override
        public void run() {
               final long start = 0;
//             long millis = SystemClock.uptimeMillis() ;
               long millis = SystemClock.uptimeMillis() - start;
               int seconds = (int) (millis / 1000);
               int minutes = seconds / 60;
               seconds     = seconds % 60;

               if (seconds < 10) {
                   waitingTime_Time.setText("" + minutes + ":0" + seconds);
               } else {
                   waitingTime_Time.setText("" + minutes + ":" + seconds);            
               }

               mHandler.postAtTime(this,
                       start + (((minutes * 60) + seconds + 1) * 1000));
           }
      };    

mStartTime = System.currentTimeMillis(); - returns the value much more than mStartTime = System.currentTimeMillis();
and that's cause negative value to update. But with setting start time to zero, timer updates every 100 millisec.

like image 143
Roger Alien Avatar answered Jul 19 '26 03:07

Roger Alien