android - Kotlin 中的 Intent 服务

标签 android kotlin

我想在 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/

相关文章:

android - 使用 CoordinatorLayout 和 AppBarLayout 查看动画变得疯狂(有时)

java - Android 上的“filenotfound”异常

android - 协程在取消时注销接收者

java - 如何使用 Google 登录验证我的应用程序?

java - 如何在 Android 上将 List<String> 设置为 ImageView 的 RecyclerView 适配器

gradle - 卡普特 : How to process test sources?

android - 手动设置Firebase Firestore缓存

android - 如何消除在文本字段之间切换时的键盘闪烁?

android - 生成签名的APK时如何解决依赖关系

java - 软键盘: Adjust_Resize pushing content offscreen