为什么跳过秒?倒计时器

时间:2021-10-09 02:47:09

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;

如果我的计数器超过30秒,它通常会在这里和那里跳过一秒钟。我把它倒数,显示屏显示。但它选择跳过随机数。它跳过的数字每次都是相同的数字。如果我改变计时器的长度(用户这样做),它会改变跳过的数字。我发布了我的代码;

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


6  

sec = (millisUntilFinished / 1000) % 60;

sec =(millisUntilFinished / 1000)%60;

Integer divided by integer produces integer, likely rounding error.

整数除以整数会产生整数,可能是舍入误差。

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

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

#1


6  

sec = (millisUntilFinished / 1000) % 60;

sec =(millisUntilFinished / 1000)%60;

Integer divided by integer produces integer, likely rounding error.

整数除以整数会产生整数,可能是舍入误差。

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

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