代码之家  ›  专栏  ›  技术社区  ›  Manos Rasidakis

倒计时计时器计数不正确

  •  0
  • Manos Rasidakis  · 技术社区  · 6 年前

    当我暂停计时器,然后再次启动它时,计时器似乎再次计数当前秒数。例如,时间是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 回复  |  直到 6 年前
        1
  •  0
  •   Jaspreet Kaur    6 年前

    你可以用计时器来显示记录时间。启动 记录时开始计时,记录时停止计时

    <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
    
        2
  •  0
  •   Jaspreet Kaur    6 年前

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

        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);
        }