Countdown timer with pause and resume

后端 未结 8 902
小蘑菇
小蘑菇 2021-01-05 04:17

I want to do countdown timer with pause and restart.Now i am displaying countdown timer By implenting ontick() and onfinish().please help me out.HEre is th code for countdow

相关标签:
8条回答
  • 2021-01-05 04:43

    I am afraid that it is not possible to pause or stop CountDownTimer and pausing or stopping in onTick has no effect whatsoever user TimerTask instead.

    Set up the TimerTask

    class UpdateTimeTask extends TimerTask {
       public void run() {
           long millis = System.currentTimeMillis() - startTime;
           int seconds = (int) (millis / 1000);
           int minutes = seconds / 60;
           seconds     = seconds % 60;
    
           timeLabel.setText(String.format("%d:%02d", minutes, seconds));
       }
    
    }
    if(startTime == 0L) {
       startTime = evt.getWhen();
       timer = new Timer();
       timer.schedule(new UpdateTimeTask(), 100, 200);
    }
    

    You can add event listener's like this..

    private Handler mHandler = new Handler();
    
    ...
    
    OnClickListener mStartListener = new OnClickListener() {
       public void onClick(View v) {
           if (mStartTime == 0L) {
                mStartTime = System.currentTimeMillis();
                mHandler.removeCallbacks(mUpdateTimeTask);
                mHandler.postDelayed(mUpdateTimeTask, 100);
           }
       }
    };
    
    OnClickListener mStopListener = new OnClickListener() {
       public void onClick(View v) {
           mHandler.removeCallbacks(mUpdateTimeTask);
       }
    };
    

    For more refer to Android Documentation.

    0 讨论(0)
  • 2021-01-05 04:43
    public class MainActivity extends AppCompatActivity {
    
    TextView textView;
    
    CountDownTimer ctimer;
    boolean runCountDown;
    private long leftTime;
    private static final long MILL_IN_FUTURE = 6000;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        textView = findViewById(R.id.text_view);
        textView.setText("Click to start");
        textView.setOnClickListener(this::clickStartAndPauseAndResume);
        leftTime = MILL_IN_FUTURE;
    
    }
    
    
    public void clickStartAndPauseAndResume(View view) {
    
        if (!runCountDown) {
    
    
            long time = (leftTime == 0 || leftTime == MILL_IN_FUTURE) ? MILL_IN_FUTURE : leftTime;
            ctimer = new CountDownTimer(time, 1) {
    
                @Override
                public void onTick(long l) {
    
                    leftTime = l;
                    textView.setText(l + "ms");
                }
    
                @Override
                public void onFinish() {
    
                    textView.setText("Done");
                    leftTime = 0;
                    runCountDown = false;
                    textView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText("Click to start");
                        }
                    }, 1000);
                }
            }.start();
            runCountDown = true;
        } else {
            ctimer.cancel();
            textView.setText(textView.getText() + "\n Click to resume");
            runCountDown = false;
        }
     }
    }
    
    0 讨论(0)
提交回复
热议问题