Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6191639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:48:07+00:00 2026-05-24T02:48:07+00:00

Possible Duplicate: How come millisUntilFinished cannot detect exact CountDownTimer intervals? Why does using the

  • 0

Possible Duplicate:
How come millisUntilFinished cannot detect exact CountDownTimer intervals?

Why does using the CountDownTimer show “1” twice? I just want it to countdown smoothly and not look like it is getting hung up on the last second. Anybody have any ideas on how to solve this problem??

Here is the code from the Android Developers page:

  new CountdownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

I created a new project and copied and pasted this code in just to make sure that I wasn’t accidentally messing something up in my program. I’ve been testing this on a Tab 10.1. It does this when I run it: “5”, “4”, “3”, “2”, “1”, “1”, “done!”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:48:07+00:00Added an answer on May 24, 2026 at 2:48 am

    The CountDownTimer is not precise, it will return as close to 1 second (in this case) as it can but often that won’t be enough to give you what you want.

    If you Log the ms var you will see that the result is something like 29384 for the first hit, and so on, it will never be 30000, 29000 etc. There is no way to get the precision you need to make the counter work as you intended as currently implemented.

    You can fix this for most cases. First lower you interval so that you get more updates than what you want to output, so instead of 1 second do 100ms and that should get you the illusions of a 1 second countdown timer since you will get at least a few updates per second (worst case)

    To optimize that bit round the result on each entry and do a simple check to see if you should do the update (secondsLeft has changed) and if it has update the UI and the secondsLeft accumulator. Also I suggest that onFinish produce the final result (0) just in case the system glitches because something is happening that eats cpu cycles.

    Anyway here is the modified code that works for this 99.99 times out of a 100 if your system is not loaded heavily. NOTE: You can probably get away with 250ms, up to you really.

     int secondsLeft = 0; 
    
     new CountDownTimer(30000, 100) {
         public void onTick(long ms) {
             if (Math.round((float)ms / 1000.0f) != secondsLeft)
             {  
                 secondsLeft = Math.round((float)ms / 1000.0f);
                 TV.setText("seconds remaining: " +secondsLeft );
             }
             Log.i("test","ms="+ms+" till finished="+secondsLeft );
         }
    
         public void onFinish() {
             TV.setText("0");
         }
    }.start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Where does ‘Hello world’ come from? What was the first Hello World
Possible Duplicate: Where does ‘Hello world’ come from? Hello world! is the most commonly
Possible Duplicate: How come MD5 hash values are not reversible? I was reading a
Possible Duplicate: C: How come an array’s address is equal to its value? SA
Possible Duplicate: Why catch and rethrow Exception in C#? I sometimes come across C#
Possible Duplicate: What is a computer programming language? No, really. I've struggled to come
Possible Duplicate: What is the “??” operator for? I have recently come across the
Possible Duplicate: Pure virtual functions may not have an inline definition. Why? I've come
Possible Duplicate: When should you use 'friend' in C++? I have come to a
Possible Duplicate: What's the difference between 'int?' and 'int' in C#? I've come across

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.