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

  • SEARCH
  • Home
  • 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 8564061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:06:39+00:00 2026-06-11T17:06:39+00:00

If I have my counter up over 30 seconds, it often skips a second

  • 0

If I have my counter up over 30 seconds, it often skips a second here and there. I have it counting down, with the display showing. But it chooses to skip random numbers. The numbers it skips are the same number each time. If I change the length of the timer (the user does that), it changes what number gets skipped. I posted my code;

Disclaimer: It is my first (useful) app, so it might suck really bad or not be structured well. That’s okay, let me know. I can take the criticism. But take a look, and if anyone has an idea why certain seconds go missing please pipe up. Thanks!

package com.ApphHose.runnersTimer;

//import a bunch of stuff

public class MainActivity extends Activity {
TextView text, lapNumber;
Button start;
boolean workrest, vibe;
int letsRun, lapCounter;
long length;
CountDownTimer timer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (TextView) findViewById(R.id.tv);
    lapNumber = (TextView) findViewById(R.id.tvLap);
    start = (Button) findViewById(R.id.bStart);
    letsRun = 2;
    lapCounter = 1;
    workrest = true;
    vibe = true;

    start.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);

    start.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            start.setText("Stop");
            start.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
            lapNumber.setText("Lap Number: 1");
            lapCounter = 0;
            run();
        }

    });

}

private void run() {

    SharedPreferences getSetup = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    String setRun = getSetup.getString("run", "1");
    long setRunset = Long.parseLong(setRun);
    setRunset = setRunset * 1000;

    String setWalk = getSetup.getString("walk", "1");
    long setWalkset = Long.parseLong(setWalk);
    setWalkset = setWalkset * 1000;

    if (letsRun >= lapCounter) {

        if (workrest == true) {
            workrest = false;
            length = setRunset;

        } else {
            workrest = true;
            length = setWalkset;
        }

        final CountDownTimer timer = new CountDownTimer(length, 1000) {

            @Override
            public void onFinish() {

                if (workrest){
                    if (vibe){
                        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                        v.vibrate(2000);
                        run();
                    } else {
                        run();
                    }

                }else{
                lapCounter ++;
                lapNumber.setText("Lap Number: " + lapCounter);
                run();
                }
            }

            @Override
            public void onTick(long millisUntilFinished) {
                long min, sec;
                // TODO Auto-generated method stub
                min = millisUntilFinished / (60 * 1000);
                sec = (millisUntilFinished / 1000) % 60;
                String tmr = String.format("%d:%02d", min, sec);
                text.setText("" + tmr);

            }

        };
        timer.start();
        start.setOnClickListener(new View.OnClickListener() {
            //this is my button. start and stop. 
            public void onClick(View v) {
                if (start.getText() == "Stop") {
                    start.setText("Start");
                    lapCounter = 1;
                    start.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
                    timer.cancel();
                    lapNumber.setText("Lap Number: 1");
                    text.setText("Stopped");
                } else {
                    start.setText("Stop");
                    start.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
                    run();
                }
            }
        });

    } else {
        text.setText("Done!");
        lapCounter = 1;

    }

}

// pop up menu
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu);
    MenuInflater blowUp = getMenuInflater();
    blowUp.inflate(R.menu.setup, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.setup:
        Intent i = new Intent("com.ApphHose.runnersTimer.SETUP");
        startActivity(i);
        break;
    case R.id.out:
        finish();
        break;
    }
    return false;
}
}
  • 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-06-11T17:06:40+00:00Added an answer on June 11, 2026 at 5:06 pm

    sec = (millisUntilFinished / 1000) % 60;

    Integer divided by integer produces integer, likely rounding error.

    sec = (millisUntilFinished / (double) 1000) % 60;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a counter that counts up every 1 second and add 1 to
I have a really simple JS counter which I display on a dashboard like
I have a counter on a 16 bits field that is incremented/decremented over the
I have a counter hash that I am trying to sort by count. The
I have a counter in a JPanel in my java applet. I want the
So I have a counter. It is supposed to calculate the current amount of
I have a performance counter category. The counters in this category may change for
i have a column family use counter as create table command below: (KEY i
I have created a multi-instance performance counter and I pass it mixed case instance
I have a need for a counter of type long with the following requirements/facts:

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.