leithdm
4/20/2020 - 7:21 AM

Timer: using a [Handler], [Runnable] versus a [CountDownTimer]

Timer: using a [Handler], [Runnable] versus a [CountDownTimer]

       //using a Handler, Runnable
        final Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Log.i("Hey its me", "a second has passed by");
                handler.postDelayed(this, 1000);
            }
        };
        handler.post(runnable);
        
        
        //using a CountDownTimer
            new CountDownTimer(10000, 1000) {
            @Override
            public void onTick(long millisecondsUntilDone) {
            Log.i("Seconds left", String.valueOf(millisecondsUntilDone/1000));
            }

            @Override
            public void onFinish() {
            Log.i("we are done", "over");
            }
        }.start();