现在 Handler() 已被弃用,我该使用什么? [英] What do I use now that Handler() is deprecated?

查看:267
本文介绍了现在 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 在构造函数中指定 Looper.getMainLooper() 方法.

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天全站免登陆