我想在 kotlin 中启动一个 Intent 服务,但我的服务有一个参数,即演示者对象。现在我们需要一个默认的无参数构造函数来进行 Intent 服务。我怎样才能在 kotlin 中实现这一目标?
有没有办法可以重载构造函数?我认为我无法在构造函数中传递演示者的默认值
class SongIdentifyService(discoverPresenter : DiscoverPresenter) : IACRCloudListener , IntentService("SongIdentifyService") {
private val callback : SongIdentificationCallback = discoverPresenter
private val mClient : ACRCloudClient by lazy(LazyThreadSafetyMode.NONE) { ACRCloudClient() }
private val mConfig : ACRCloudConfig by lazy(LazyThreadSafetyMode.NONE) { ACRCloudConfig() }
private var initState : Boolean = false
private var mProcessing : Boolean = false
override fun onHandleIntent(intent: Intent?) {
setUpConfig()
addConfigToClient()
startIdentification(callback)
}
}
最佳答案
您可以有两个像这样的辅助构造函数(简化):
class SongIdentifyService {
constructor(discoverPresenter: DiscoverPresenter)
constructor()
}
或者使discoverPresenter可为空并给它一个默认值:
class SongIdentifyService(discoverPresenter: DiscoverPresenter? = null)
关于android - Kotlin 中的 Intent 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49698144/