Android 工作经理和科丹

Android 工作经理和科丹,android,kotlin,android-workmanager,kodein,Android,Kotlin,Android Workmanager,Kodein,我在用Kodein实现android WorkManager时遇到问题。 我想使用WorkManager跟踪后台位置更新,并定期向我的服务器发送post请求和用户的新位置 我已经看到了使用Dagger2实现这一点的多种方法,但我想知道kodein是否也有类似的解决方案 这就是我得到的错误: 2019-09-23 12:44:06.690 17462-17500/com.rainparrot.app E/WM worker工厂:无法实例化com.rainparrot.app.utility.Tra

我在用Kodein实现android WorkManager时遇到问题。 我想使用WorkManager跟踪后台位置更新,并定期向我的服务器发送post请求和用户的新位置

我已经看到了使用Dagger2实现这一点的多种方法,但我想知道kodein是否也有类似的解决方案

这就是我得到的错误:

2019-09-23 12:44:06.690 17462-17500/com.rainparrot.app E/WM worker工厂:无法实例化com.rainparrot.app.utility.TrackLocationWorker
java.lang.NoSuchMethodException:[类android.content.Context,类androidx.work.WorkerParameters]
位于java.lang.Class.getConstructor0(Class.java:2204)
位于java.lang.Class.getDeclaredConstructor(Class.java:2050)
位于androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:96)
位于androidx.work.impl.workerRapper.runWorker(workerRapper.java:228)
在androidx.work.impl.workerRapper.run(workerRapper.java:127)上
位于androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
运行(Thread.java:761)
2019-09-23 12:44:06.690 17462-17500/com.rainparrot.app E/WM workerrapper:无法创建Worker com.rainparrot.app.utility.TrackLocationWorker
2019-09-23 12:44:06.830 17462-17462/com.rainparrot.app D/main活动$onCreate:工作经理状态:失败

这是我的主应用程序文件,其中包含我的所有kodein绑定:

    override val kodein = Kodein.lazy {
    import(androidXModule(this@ForecastApplication))
    val prefs = PreferenceManager.getDefaultSharedPreferences(this@ForecastApplication)
    bind<ICurrentWeatherDao>() with singleton { CurrentWeatherDao(prefs) }
    bind<IConnectivityInterceptor>() with singleton { ConnectivityInterceptor(instance()) }
    bind() from singleton { ApiService(instance()) }
    bind<IWeatherNetworkDataSource>() with singleton { WeatherNetworkDataSource(instance(), instance()) }
    bind<ILocationService>() with singleton { LocationService() }
    bind<IForecastRepository>() with singleton { ForecastRepository(instance(), instance(), instance(), instance(), instance(), instance()) }
    bind() from provider { LocationRepository(instance()) }
    bind() from provider { TrackLocationWorker(instance(), instance(), instance()) }
    bind() from provider { WeatherViewModelFactory(instance()) }
}
MainActivity实例化:

fun getFromLocation() = if (isGPSEnabled()) forecastRepository.trackLocation() else forecastRepository.locationSetup()
预测存储库设置:

override fun locationSetup() {
        enableLocation.value = Response.loading()
        LocationServices.getSettingsClient(application)
                .checkLocationSettings(
                        LocationSettingsRequest.Builder()
                                .addLocationRequest(locationRequest())
                                .setAlwaysShow(true)
                                .build())
                .addOnSuccessListener { enableLocation.value = Response.success(true) }
                .addOnFailureListener {
                    Timber.e(it, "Gps not enabled")
                    enableLocation.value = Response.error(it)
                }
    }

    override fun trackLocation() {
        val locationWorker = PeriodicWorkRequestBuilder<TrackLocationWorker>(15, TimeUnit.MINUTES).addTag(LOCATION_WORK_TAG).build()
        WorkManager.getInstance(this@ForecastRepository.context).enqueueUniquePeriodicWork(LOCATION_WORK_TAG, ExistingPeriodicWorkPolicy.KEEP, locationWorker)
    }
覆盖有趣的位置设置(){
enableLocation.value=Response.loading()
LocationServices.getSettingsClient(应用程序)
.检查位置设置(
LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest())
.setAlwaysShow(真)
.build())
.addOnSuccessListener{enableLocation.value=Response.success(true)}
.addOnFailureListener{
Timber.e(it,“未启用Gps”)
enableLocation.value=Response.error(it)
}
}
覆盖有趣的trackLocation(){
val locationWorker=PeriodicWorkRequestBuilder(15,TimeUnit.MINUTES).addTag(LOCATION\u WORK\u TAG).build()
WorkManager.getInstance(this@ForecastRepository.context).enqueueUniquePeriodicWork(位置\工作\标记,ExistingPeriodicWorkPolicy.KEEP,位置工作者)
}

您正在声明绑定,但似乎没有检索到它们……例如,如果您从提供程序{TrackLocationWorker(instance(),instance(),instance())声明
bind(),则要使用它,您应该执行类似于kodein.instance()的
val locationWorker:TrackLocationWorker的操作
@romainbsl我应该把
val locationWorker:TrackLocationWorker by kodein.instance()
放在哪里,你不需要实例化它。你可以在这里找到示例
override fun locationSetup() {
        enableLocation.value = Response.loading()
        LocationServices.getSettingsClient(application)
                .checkLocationSettings(
                        LocationSettingsRequest.Builder()
                                .addLocationRequest(locationRequest())
                                .setAlwaysShow(true)
                                .build())
                .addOnSuccessListener { enableLocation.value = Response.success(true) }
                .addOnFailureListener {
                    Timber.e(it, "Gps not enabled")
                    enableLocation.value = Response.error(it)
                }
    }

    override fun trackLocation() {
        val locationWorker = PeriodicWorkRequestBuilder<TrackLocationWorker>(15, TimeUnit.MINUTES).addTag(LOCATION_WORK_TAG).build()
        WorkManager.getInstance(this@ForecastRepository.context).enqueueUniquePeriodicWork(LOCATION_WORK_TAG, ExistingPeriodicWorkPolicy.KEEP, locationWorker)
    }