我创建了一个Java类的Kotlin子类:
class AlarmReceiver : WakefulBroadcastReceiver() {
companion object {
const val ACTION_NOTIFY = "..."
}
override fun onReceive(context: Context, intent: Intent) { ... }
}
WakefulBroadcastReceiver
has two static methods:
static boolean completeWakefulIntent(Intent intent)
static ComponentName startWakefulService(Context context, Intent intent)
在我的AlarmReceiver
个班级里打电话给他们,效果和我预期的一样.然而,我想将其中一种方法称为outside of my Kotlin subclass.
The Problem
If I try AlarmReceiver.completeWakefulIntent(intent)
from a different Kotlin class, I get the following compilation error:
未解析的引用:completeWakefulIntent
I think this is because the compiler is trying to resolve the method on AlarmReceiver
's companion object instead of finding the inherited method from its superclass. As a workaround, I can directly define a method with the same signature on AlarmReceiver.Companion
:
class AlarmReceiver : WakefulBroadcastReceiver() {
companion object {
const val ACTION_NOTIFY = "..."
// Just call the superclass implementation for now
fun completeWakefulIntent(intent: Intent): Boolean =
WakefulBroadcastReceiver.completeWakefulIntent(intent)
}
...
}
I think this has the same behavior I would have gotten had I relied on the default inherited method from a Java subclass, but I'm wondering if there's a better way to do this.
有没有办法在Kotlin子类上调用继承的静电Java方法?