小编Alm*_*maz的帖子

Kotlin:如何在binder中返回正在运行的服务实例?

我有Java代码,Service并试图将其转换为Kotlin.

class MyService : Service() {

    companion object MyBinder : Binder() {
        fun getService() : MyService? {
            return MyService::class.objectInstance
        }
    }

    // service implementation

}
Run Code Online (Sandbox Code Playgroud)

问题是在活动中getService()始终返回null.我确定它service是在之前启动的,我在logcat中看到它.我建议这个来自Java代码的自动生成的行应该是不同的但我找不到解决方案:

return MyService::class.objectInstance   
Run Code Online (Sandbox Code Playgroud)

在Java代码中它是:

return MyService.this 
Run Code Online (Sandbox Code Playgroud)

android kotlin

4
推荐指数
1
解决办法
1830
查看次数

标签 统计

android ×1

kotlin ×1