倒数计时器计数不正确

时间:2018-02-21 10:41:07

标签: java android timer countdown

当我暂停计时器然后再次启动时,似乎计时器再次计算当前秒。例如,它是00:10,在半秒后我停止计时器,当我再次启动时,而不是在半秒内从00:10到00:09,它计算一整秒。我找不到有什么不对。谢谢你的帮助。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer_2);   
    //...
     Timer buttonTimer = new Timer();
     buttonTimer.schedule(new TimerTask() {
          @Override
           public void run() {
            runOnUiThread(new Runnable() {                            
            if (timer_2_up_running) {
                pausetimer_2_up();
                starttimer_2_down();

            } else {
                starttimer_2_up();
                if (timer_2_down_running) {
                    pausetimer_2_down(); 


private void starttimer_2_up() {
    timer_2_up_countdowntimer = new CountDownTimer(starttimeup, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            starttimeup = millisUntilFinished;
            update_up_text();          

        @Override
        public void onFinish() {
            timer_2_up_running = false;

        }
    }.start();

    timer_2_up_running = true;
}

private void starttimer_2_down() {
    timer_2_down_countdowntimer = new CountDownTimer(starttimedown, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            starttimedown = millisUntilFinished;
            update_down_text();

        }

        @Override
        public void onFinish() {
            timer_2_down_running = false;

        }
    }.start();

    timer_2_down_running = true;
}

private void pausetimer_2_up() {
    timer_2_up_countdowntimer.cancel();
    timer_2_up_running = false;
}

private void pausetimer_2_down() {
    timer_2_down_countdowntimer.cancel();
    timer_2_down_running = false;
}

private void update_up_text() {
    int minutes_up = (int) (starttimeup / 1000) / 60;
    int seconds_up = (int) (starttimeup / 1000) % 60;
    String time_2_up_left_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up, seconds_up);
    timer_2_up.setText(time_2_up_left_formatted);
}

private void update_down_text() {
    int minutes_down = (int) (starttimedown / 1000) / 60;
    int seconds_down = (int) (starttimedown / 1000) % 60;
    String time_2_down_left_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_down, seconds_down);
    timer_2_down.setText(time_2_down_left_formatted);
}

2 个答案:

答案 0 :(得分:0)

  

您可以使用Chronometer显示录制时间。开始吧   记录开始时的计时器和记录停止时的计时器

<Chronometer
        android:id="@+id/chronometerRecordTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/relativeLayout_bottom_video_controls"
        android:layout_centerInParent="true"
        android:layout_marginBottom="20dp"
        android:visibility="invisible"/>


private Chronometer chronometerRecordTime;

chronometerRecordTime = findViewById(R.id.chronometerRecordTime);

chronometerRecordTime.setBase(SystemClock.elapsedRealtime()); // Reset
chronometerRecordTime.start(); // start

chronometerRecordTime.stop(); // stop

答案 1 :(得分:0)

  

我还有一个箱子计时器,在一分钟后停止录音。您可以   根据您的要求进行修改。

    int myTimeCounter;
    Timer myRecordingTimer;
    private boolean myTimerHasStarted = false;
    private String TAG = "Log";
    private void startTimerTask() {
        myTimeCounter = 1;
        myRecordingTimer = new Timer();
        myRecordingTimer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        myTimerHasStarted = true;
                        if (myTimeCounter == 60) {
                            myRecordingTimer.cancel();
                            myTimerHasStarted = false;
                            return;
                        }
                        Log.d(TAG, "timer=" + String.valueOf(myTimeCounter));
                        myTimeCounter++;
                    }
                });
            }
        }, 0, 1000);
    }