我是 android 开发新手,我需要注入 WorkManager 以使用 kodein,但我不知道从哪里开始
这是注入活动的方法
class MyActivity : Activity(), KodeinAware {
override val kodein by kodein()
val ds: DataSource by instance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ds.connect()
/* ... */
}
}
这是我的工人,我需要获取 Appdatabase 的实例以使用 kodein,但它对我不起作用
class MyWorker(context: Context, params: WorkerParameters) :
CoroutineWorker(context, params), KodeinAware {
override val kodein by kodein()
private val db: AppDatabase by instance()
override suspend fun doWork(): Result = withContext(IO) {
try {
//do the work
Result.success()
} catch (e: Exception) {
Result.retry()
}
}
}
绑定 AppDatabase
class AppBase : Application(), KodeinAware {
override val kodein = Kodein.lazy {
import(androidXModule(this@AppBase))
bind() from singleton { AppDatabase.getInstance(instance())}
}
}