Handler.postDelayed(Runnable)与CountdownTimer [英] Handler.postDelayed(Runnable) vs CountdownTimer

查看:130
本文介绍了Handler.postDelayed(Runnable)与CountdownTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候我们需要延迟代码才能运行。

Sometimes we need to delay a code before it runs.

这可以通过 Handler.postDelayed(Runnable) CountdownTimer


哪个在性能上更好?

Which one is better in terms of performance?

请参见下面的示例代码

处理程序

        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();


推荐答案

Handler 应该为您提供更好的性能,因为 CountDownTimer 本身包含一个 Handler ,如您所见这里

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

这篇关于Handler.postDelayed(Runnable)与CountdownTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆