Handler.postDelayed(Runnable) vs CountdownTimer

♀尐吖头ヾ 提交于 2019-12-01 15:03:54

问题


Sometimes we need to delay a code before it runs.

This is doable by the Handler.postDelayed(Runnable) or CountdownTimer.

Which one is better in terms of performance?

See the sample code below

Handler

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                 //DO SOMETHING
            }
        }, 1000);

CountDownTimer

        new CountDownTimer(1000, 1000) {
            public void onFinish() {
                 //DO SOMETHING
            }
            public void onTick(long millisUntilFinished) {}
        }.start();

回答1:


The Handler should offer you better performances as CountDownTimer contains itself a Handler as you can see here.




回答2:


Use Handler,Android Handler is Good.

See Here, What Others say About Handler




回答3:


I agree that Handler is offering a better performance. But on a side note, you should keep in mind that CountDownTimer object will be destroyed after completed. A Handler will continue to exist after finished. If you only need a temporary timer then CountDownTimer is preferable. Otherwise, use a Handler.



来源:https://stackoverflow.com/questions/35497844/handler-postdelayedrunnable-vs-countdowntimer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!