Android: How to pause and resume a Count Down Timer?

后端 未结 8 1632
长发绾君心
长发绾君心 2020-12-02 17:20

I have developed a Count Down Timer and I am not sure how to pause and resume the timer as the textview for the timer is being clicked. Click to start then click again to pa

8条回答
  •  孤城傲影
    2020-12-02 17:50

    You can try using a library I created, Hourglass

    Hourglass hourglass = new Hourglass(50000, 1000) {
            @Override
            public void onTimerTick(long timeRemaining) {
                // Update UI
                Toast.show(MainActivity.this, String.valueOf(timeRemaining), Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onTimerFinish() {
                // Timer finished
                Toast.show(MainActivity.this, "Timer finished", Toast.LENGTH_SHORT).show();
    
    
            }
        };
    

    Use hourglass.startTimer(); to start the timer.

    It has helper methods which allow to pause and resume the timer.

    hourglass.pauseTimer();
    

    AND

    hourglass.resumeTimer();
    

提交回复
热议问题