倒计时计时器在倒计时期间的不同时间段内跳过一秒

时间:2020-12-20 15:16:37

标签: time countdowntimer

我有一个倒数计时器,使用类 CountdownTimer。我将一个时间段从一个活动发送到我有计时器的那个活动。我还有一个进度条,我也使用倒数计时器计算其进度。它应该每秒倒计时,所以 1000 毫秒。

问题是它总是在某个时候跳过 1 秒。它是随机发生的,我试图找出问题所在。

这是我的代码的一部分:

      private void startTimer()
    {
        millisInFuture = mTimeLeftInMillis;
        mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, intervaltime) {
            @Override
            public void onTick(long millisUntilFinished) {

                        mTimeLeftInMillis = Math.max(millisUntilFinished, millisUntilFinished - 1000);
                        updateCountDownText();
                        long millisPassed = millisInFuture - mTimeLeftInMillis;
                Log.println(Log.VERBOSE,"CHECK","millisUntilFinished = "+millisUntilFinished);
                Log.println(Log.VERBOSE,"CHECK","time in the future= "+millisInFuture);
                Log.println(Log.VERBOSE,"CHECK","time left= "+mTimeLeftInMillis);
                Log.println(Log.VERBOSE,"CHECK","Millispassed= "+millisPassed);
                Log.println(Log.VERBOSE,"CHECK","Start Time = "+START_TIME_IN_MILLIS);

                progress = (int) (millisPassed * 100 / millisInFuture);
                        pb.setProgress(progress);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////


                //Key: 60 sec
                        if (millisInFuture == 720000) {
                            if (millisPassed <= 60000 || millisPassed > 660000 && millisPassed <= 720000) {
                                Log.println(Log.ASSERT, "CHECK", "Check that the first if statement of key 60 is entered");
                                statusIfUp();
                                time_of_stage = ((millisInFuture - millisPassed) % 60000);
                                progress2 = (int) (time_of_stage * 100 / 60000);
                                Log.println(Log.VERBOSE, "CHECK", "TIME OF STAGE = " + time_of_stage);
                                Log.println(Log.VERBOSE, "CHECK", "progress2= " + progress2);
                                pb2.setProgress(progress2);
                                updateStageUpCount();
                                upArrowAnimation();

                                setflowrate();
                            } else if (millisPassed > 60000 && millisPassed <= 180000) {
                                Log.println(Log.ASSERT, "CHECK", "Check that the second if statement of key 60 is entered");
                                statusIfDown();

                                time_of_stage = ((millisInFuture + 60000) - millisPassed) % 120000;    //CREDIT GOES TO BASHMOHNDES AMIN
                                progress3 = (int) (time_of_stage * 100 / 120000);
                                Log.println(Log.VERBOSE, "CHECK", "TIME OF STAGE = " + time_of_stage);
                                Log.println(Log.VERBOSE, "CHECK", "progress3= " + progress3);
                                pb3.setProgress(progress3);
                                updateStageDownCount();
                                downArrowAnimation();

                                setFlowratetozero();
                            } else if (millisPassed > 180000 && millisPassed <= 300000) {
                                Log.println(Log.ASSERT, "CHECK", "Check that the first if statement of key 60 is entered");
                                statusIfUp();
                                time_of_stage = ((millisInFuture + 60000) - millisPassed) % 120000;
                                progress2 = (int) (time_of_stage * 100 / 120000);
                                Log.println(Log.VERBOSE, "CHECK", "TIME OF STAGE = " + time_of_stage);
                                Log.println(Log.VERBOSE, "CHECK", "progress2= " + progress2);
                                pb2.setProgress(progress2);
                                updateStageUpCount();
                                upArrowAnimation();

                                setflowrate();
                            } else if (millisPassed > 300000 && millisPassed <= 360000 || millisPassed > 600000 && millisPassed <= 660000) {
                                Log.println(Log.ASSERT, "CHECK", "Check that the second if statement of key 60 is entered");
                                statusIfDown();

                                 time_of_stage = (millisInFuture - millisPassed) % 60000;    //CREDIT GOES TO BASHMOHNDES AMIN
                                progress3 = (int) (time_of_stage * 100 / 60000);
                                Log.println(Log.VERBOSE, "CHECK", "TIME OF STAGE = " + time_of_stage);
                                Log.println(Log.VERBOSE, "CHECK", "progress3= " + progress3);
                                pb3.setProgress(progress3);
                                updateStageDownCount();
                                downArrowAnimation();

                                setFlowratetozero();
                            } else if (millisPassed > 360000 && millisPassed <= 600000) {
                                Log.println(Log.ASSERT, "CHECK", "Check that the first if statement of key 60 is entered");
                                statusIfUp();
                                time_of_stage = ((millisInFuture + 120000) - millisPassed) % 240000;
                                progress2 = (int) (time_of_stage * 100 / 240000);
                                Log.println(Log.VERBOSE, "CHECK", "TIME OF STAGE = " + time_of_stage);
                                Log.println(Log.VERBOSE, "CHECK", "progress2= " + progress2);
                                pb2.setProgress(progress2);
                                updateStageUpCount();
                                upArrowAnimation();

                                setflowrate();
                            }
                        }
          }

            @Override
            public void onFinish()
            {
                Toast.makeText(tool2mode4.this, "Done", Toast.LENGTH_SHORT).show();
                avd2.stop(); avd3.stop();
                try {
                    pb.setProgress(100); pb2.setProgress(0); pb3.setProgress(0);
                    stage_timer.setVisibility(View.INVISIBLE);
                    stage_timer.setVisibility(View.INVISIBLE);
                    progressBar.setVisibility(View.INVISIBLE);
                    progressBar2.setVisibility(View.INVISIBLE);
                    progressBar.setProgress(0);
                    progressBar2.setProgress(0);
                    progressBar.setTranslationY(60);
                    progressBar2.setTranslationY(60);
                    flow.setTranslationY(60);
                    animation1.cancel(); animation2.cancel();
                    //Vibration


                    if (Build.VERSION.SDK_INT >= 26) {
                        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(1500, VibrationEffect.DEFAULT_AMPLITUDE));
                    } else {
                        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createWaveform(new long[]{1500}, new int[]{VibrationEffect.EFFECT_CLICK}, -1));
                    }
                }
                catch (NullPointerException e) {
                    e.printStackTrace();
                }

            }
        }.start();

我的计时器有点大,因为它包含两个较小的计时器,它们在总时间中对个别分钟进行倒计时,并在这些时间内激活动画。

我发现 OnTick 方法中 millisUntilFinished 的值不等于倒计时开始的时间。意思是如果开始时间是 720000,那么millisUntilFinished 是 719971。

我不知道这意味着什么,但我的目标是让它每秒倒计时,而不会跳过一秒。知道我应该做什么吗?

0 个答案:

没有答案