如何解决不推荐使用的Handler()? [英] How to solve Handler() deprecated?

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

问题描述

任何人都可以知道,如何解决不建议使用的警告或对此问题的替代解决方案.

Could anyone know, how to fix deprecated waring or any alternate solution for this.

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

推荐答案

如果您想避免Kotlin中的空检查内容(?!!),可以使用Looper.getMainLooper()(如果您的Handler工作正常)与UI相关的东西,例如:

If you want to avoid the null check thing in Kotlin (? or !!) you can use Looper.getMainLooper() if your Handler is working with some UI related thing, like this:

Handler(Looper.getMainLooper()).postDelayed({
   Toast.makeText(this@MainActivity, "LOOPER", Toast.LENGTH_SHORT).show()
}, 3000)

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

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