首页 > 解决方案 > Android Studio中的倒数计时器在时间间隔内跳过了一些秒

问题描述

一天前我读到了这个问题,问题发生在人们身上,但到了最后一秒。我一直注意到我的计时器在倒计时时有时会跳过一秒钟,有些人建议将间隔从 1000 毫秒更改为 500 毫秒,其他人说让它变成 900。还有一些人建议制作自己的计时器。

我的计时器有点大,因为它包含两个较小的计时器,它们在总时间内倒计时各个分钟,并在这些时间内激活动画。所以基本上我在问我应该采取什么方法来获得 100% 准确的时间关键计数器?

这是我的代码:

        private void startTimer() {
        Log.println(Log.ASSERT, "CHECK","Entered startTimer() method");
        millisInFuture = mTimeLeftInMillis;
        mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 900) {
            @Override
            public void onTick(long millisUntilFinished) {
                mTimeLeftInMillis = millisUntilFinished;
                updateCountDownText();
                millisPassed = millisInFuture - mTimeLeftInMillis;
                progress = (int) (millisPassed * 100 / millisInFuture);
                pb.setProgress(progress);
                pb2.setProgress(0);
                pb3.setProgress(0);


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                //Key: 60 sec
                if (millisInFuture == 480000) {
                    if (millisPassed <= 60000 || (millisPassed > 180000 && millisPassed <= 240000) || (millisPassed > 300000 && millisPassed <= 360000 || (millisPassed > 420000 && millisPassed <= 480000))) {
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this, R.anim.fade_in);
//                        stepupimage.setAnimation(animation);

                        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();
}
}
    @Override
            public void onFinish() {
                Toast.makeText(tool1mode1.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(150, VibrationEffect.DEFAULT_AMPLITUDE));
                    } else {
                        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createWaveform(new long[]{150}, new int[]{VibrationEffect.EFFECT_CLICK}, -1));
                    }
                }
                catch (NullPointerException e) {
                    e.printStackTrace();
                }

            }
        }.start();

    }

总结一下:我有一个大计时器和两个小计时器,它们在较大的计时器中计算某些间隔。现在,Android 提供的CountDownTimer类在倒计时期间有时会跳过一两秒。我想解决这个问题,但不知道如何解决,我本来只是想解决这个问题,但不幸的是,我正在为其制作应用程序的应用程序要求它 100% 准确,因为应用程序对时间要求很高。

注:pb、pb2、pb3 是定时器的进度条(第一个是大的,第二个和第三个交替)

标签: countdowntimer

解决方案


推荐阅读