如何在Kotlin中使用Handler和handleMessage? [英] How to use Handler and handleMessage in Kotlin?

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

问题描述

Java代码:

private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
       // code here
    }
};

如何将此Java代码转换为Kotlin?

How to convert this java code to Kotlin?

我尝试过:

private val mHandler = object : Handler() {
    fun handleMessage(msg: Message) {
       // code here
    }
}

但这似乎是不正确的,并且在object

But this is seems to be incorrect and gives a compile time error on object

推荐答案

问题:覆盖Handler类的handleMessage()方法的语法不正确.

Problem: The syntax to override handleMessage() method of the Handler class is incorrect.

解决方案::在要覆盖的功能之前添加override关键字.

Solution: Add override keyword before the function that you want to override.

private val mHandler = object : Handler() {

    override fun handleMessage(msg: Message?) {
        // Your logic code here.
    }
}

更新:作为@BeniBela的评论,使用上述代码时,将显示皮棉警告

Update: As @BeniBela's comment, when using above code, a lint warning will be displayed

此Handler类应该是静态的,否则可能会发生泄漏.

This Handler class should be static or leaks might occur.

由于此Handler被声明为内部类,因此可能会阻止 外层类被垃圾收集.如果处理程序正在使用 对于主线程以外的线程,则使用Looper或MessageQueue,然后 没有问题.

Since this Handler is declared as an inner class, it may prevent the outer class from being garbage collected. If the Handler is using a Looper or MessageQueue for a thread other than the main thread, then there is no issue.

如果Handler使用主线程的Looper或MessageQueue, 您需要修改您的Handler声明,如下所示: 处理程序作为静态类;在外部类中,实例化一个 对外部类的WeakReference并将此对象传递给您的Handler 当您实例化处理程序时;引用所有成员 外部类使用WeakReference对象.

If the Handler is using the Looper or MessageQueue of the main thread, you need to fix your Handler declaration, as follows: Declare the Handler as a static class; In the outer class, instantiate a WeakReference to the outer class and pass this object to your Handler when you instantiate the Handler; Make all references to members of the outer class using the WeakReference object.

class OuterClass {

    // In the outer class, instantiate a WeakReference to the outer class.
    private val outerClass = WeakReference<OuterClass>(this)

    // Pass the WeakReference object to the outer class to your Handler
    // when you instantiate the Handler
    private val mMyHandler = MyHandler(outerClass)

    private var outerVariable: String = "OuterClass"

    private fun outerMethod() {

    }

    // Declare the Handler as a static class.
    class MyHandler(private val outerClass: WeakReference<OuterClass>) : Handler() {

        override fun handleMessage(msg: Message?) {
            // Your logic code here.
            // ...

            // Make all references to members of the outer class 
            // using the WeakReference object.
            outerClass.get()?.outerVariable
            outerClass.get()?.outerMethod()
        }
    }
}

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

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