无法在Kotlin中覆盖Java函数 [英] Cannot override Java function in Kotlin

查看:147
本文介绍了无法在Kotlin中覆盖Java函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Kotlin开发针对API 27的启用BLE的Android应用.

I am currently developing a BLE-enabled Android app targeting API 27 using Kotlin.

我正在尝试覆盖android.bluetooth.BluetoothGatt中的函数.有许多回调可被覆盖以启用某些BLE事件的处理.

I am attempting to override a function within android.bluetooth.BluetoothGatt. There are a number of callbacks available to be overridden to enable the handling of certain BLE events.

例如,我以以下方式覆盖onConnectionStateChange():

For example, I override onConnectionStateChange() in the following way:

private val bluetoothGattCallback = object : BluetoothGattCallback() {

    override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
        /* do stuff */
    }

这很好用.

我的问题源于试图覆盖onConnectionUpdated().此回调的定义方式与

My issue stems from trying to override onConnectionUpdated(). This callback is defined in the same way as onConnectionStateChange() in the BLE API source, so how come I can't override it? This is how I am attempting to override it (still within the BluetoothGattCallback() object):

fun onConnectionUpdated(gatt: BluetoothGatt, interval: Int, latency: Int, timeout: Int, status: Int) {
    /* do stuff */
}

我忘了提一下,当我添加override关键字时,它会提供错误消息:OnConnectionUpdated overrides nothing..

I forgot to mention that, when I add the override keyword it provides the error message: OnConnectionUpdated overrides nothing..

请原谅我的天真,谢谢.我不经常使用Kotlin/Java.

Forgive my naivety, I don't often work with Kotlin/Java, thanks.

推荐答案

您不应使用此方法,该方法仅供内部使用,而不是公共API的一部分.因此,它通过@hide隐藏.有关@hide及其如何访问的更多信息,请参见 @hide在Android源代码中是什么意思?

You should not use this method, it is only for internal use and not part of the public API. Therefore it is hidden via @hide. For more information about @hide and how to access it regardless see What does @hide mean in the Android source code?

请注意,如上面的链接所述,使用反射访问它

Note that using reflection to access it as described in the link above is discouraged

您要使用的方法位于暗灰色列表,并带有以下

The method you want to use is on the dark-greylist with the following restrictions:

黑名单:

  • 对于目标SDK低于API级别28的应用:每次使用深色
    允许使用灰名单界面.
  • 目标SDK为API级别28或更高的应用:与黑名单相同的行为
  • For apps whose target SDK is below API level 28: each use of a dark
    greylist interface is permitted.
  • apps whose target SDK is API level 28 or higher: same behavior as blacklist

黑名单:不受目标SDK限制.该平台的行为就像没有界面一样.例如,当应用程序尝试使用它时,它将抛出NoSuchMethodError/NoSuchFieldException,而当应用程序想知道特定类的字段/方法列表时,它将不包含它.

blacklist: restricted regardless of target SDK. The platform will behave as if the interface is absent. For example, it will throw NoSuchMethodError/NoSuchFieldException whenever the app is trying to use it, and will not include it when the app wants to know the list of fields/methods of a particular class.

这篇关于无法在Kotlin中覆盖Java函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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