简体   繁体   English

无法覆盖Kotlin中的Java函数

[英]Cannot override Java function in Kotlin

I am currently developing a BLE-enabled Android app targeting API 27 using Kotlin. 我目前正在使用Kotlin开发针对API 27的支持BLE的Android应用。

I am attempting to override a function within android.bluetooth.BluetoothGatt . 我试图覆盖android.bluetooth.BluetoothGatt一个函数。 There are a number of callbacks available to be overridden to enable the handling of certain BLE events. 可以覆盖许多回调以启用某些BLE事件的处理。

For example, I override onConnectionStateChange() in the following way: 例如,我以下列方式覆盖onConnectionStateChange()

private val bluetoothGattCallback = object : BluetoothGattCallback() {

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

This works just fine. 这很好用。

My issue stems from trying to override onConnectionUpdated() . 我的问题源于尝试覆盖onConnectionUpdated() This callback is defined in the same way as onConnectionStateChange() in the BLE API source , so how come I can't override it? 此回调的定义方式与BLE API源中的onConnectionStateChange()相同,那么为什么我不能覆盖它呢? This is how I am attempting to override it (still within the BluetoothGattCallback() object): 这就是我试图覆盖它的方式(仍然在BluetoothGattCallback()对象中):

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

EDIT: I forgot to mention that, when I add the override keyword it provides the error message: OnConnectionUpdated overrides nothing. 编辑:我忘了提到,当我添加override关键字时,它提供了错误消息: OnConnectionUpdated overrides nothing. .

Forgive my naivety, I don't often work with Kotlin/Java, thanks. 原谅我的天真,我不经常与Kotlin / Java合作,谢谢。

You should not use this method, it is only for internal use and not part of the public API. 您不应该使用此方法,它仅供内部使用而不是公共API的一部分。 Therefore it is hidden via @hide . 因此它通过@hide隐藏。 For more information about @hide and how to access it regardless see What does @hide mean in the Android source code? 有关@hide以及如何访问它的更多信息,请参阅Android源代码中的@hide是什么意思?

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 : 您要使用的方法是在暗灰色列表上,具有以下限制

dark-greylist : 黑暗灰名单

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

blacklist : restricted regardless of target SDK. 黑名单 :无论目标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. 例如,每当应用程序尝试使用它时,它都会抛出NoSuchMethodError / NoSuchFieldException,并且当应用程序想要知道特定类的字段/方法列表时,它不会包含它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM