Implementing counter in Android

前端 未结 2 1225
终归单人心
终归单人心 2021-01-07 07:05

I have got an application where I need to show counter from 3 to 1 then quickly switch to another activity. Will TimerTask will be suitable for doing this? Can anybody show

2条回答
  •  暖寄归人
    2021-01-07 07:24

    Use CountDownTimer as shown below.

    Step1: create CountDownTimer class

    class MyCount extends CountDownTimer {
            public MyCount(long millisInFuture, long countDownInterval) {
                super(millisInFuture, countDownInterval);
            }
    
            public void onFinish() {
                dialog.dismiss();
               // Use Intent to Navigate from this activity to another
            }
    
            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
            }
    }
    

    Step2: create an object for that class

    MyCount counter = new MyCount(2000, 1000); // set your seconds
    counter.start();
    

提交回复
热议问题