现在不推荐使用Handler(),我该怎么用? [英] What do I use now that Handler() is deprecated?

查看:1422
本文介绍了现在不推荐使用Handler(),我该怎么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决此代码中的弃用警告?另外,还有其他选择吗?

How do I fix the deprecation warning in this code? Alternatively, are there any other options for doing this?

Handler().postDelayed({
    context?.let {
        //code
    }
}, 3000)

推荐答案

仅不建议使用无参数构造函数,现在最好通过Looper.getMainLooper()方法在构造函数中指定Looper.

Only the parameterless constructor is deprecated, it is now preferred that you specify the Looper in the constructor via the Looper.getMainLooper() method.

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
    @Override
    public void run() {
        // Your Code
    }
}, 3000);

将其用于Kotlin

Handler(Looper.getMainLooper()).postDelayed({
    // Your Code
}, 3000)

这篇关于现在不推荐使用Handler(),我该怎么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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