countdowntimer - 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 是定时器的进度条(第一个是大的,第二个和第三个交替)
解决方案
推荐阅读
- akka - 在akka集群中关闭节点/成员的正确方法
- python - ffmpeg 自动重新缩放。如何在不必为“force_original_aspect_ratio”提供高度的情况下防止它?
- javascript - 将产品添加到 Stripe 费用?
- javascript - 如何在 nodejs 服务器上使用 tensorflow js?
- python - python:从基类调用派生的__init__,多个构造函数问题
- python - 如何在循环中选中多个复选框
- php - php中的角色和权限
- c# - 如何在 Unity 中向对象添加力,而不是相对于对象旋转而是相对于游戏坐标?
- php - 如何实现在播放实际视频之前显示的 YouTube 和 Netflix 等视频广告
- asp.net-core-mvc - int 不包含 getAwaiter 错误的定义