Custom WorkerManager
class CustomWorker(
val
context: Context, workerParams: WorkerParameters
) : Worker(context, workerParams) {
override fun doWork(): Result {
val appDao = AppDatabase.getInstance(context).appDao()
appDao.deleteAllRecords().subscribeOn(Schedulers.io())
.observeOn(Schedulers.io()).subscribe(object : CompletableObserver {
override fun onComplete() {
Log.d("WorkManager", "onComplete")
}
override fun onSubscribe(d: Disposable) {
Log.d("WorkManager", "onSubscribe")
}
override fun onError(e: Throwable) {
Log.d("WorkManager", "onError")
}
})
Log.d("WorkManager", "WorkManager")
Looper.prepare()
context.toast("Hellow")
Looper.loop().
return Result.success()
}
}
Вызов WorkManager
fun createWorkerForClearingDb() {
val oneTimeWorkRequest =
OneTimeWorkRequest.Builder(CustomWorker::class.java)
.setInitialDelay(2L, TimeUnit.HOURS).build()
WorkManager.getInstance().beginUniqueWork(
AppConstants.Work.DELETE_TASK,
ExistingWorkPolicy.REPLACE,
oneTimeWorkRequest
).enqueue()
}
Зависимости
//room
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
implementation "androidx.room:room-ktx:$room_version"
//rx-java
implementation "io.reactivex.rxjava2:rxandroid:$rxandroid_version"
implementation "io.reactivex.rxjava2:rxkotlin:$rxkotlin_version"
implementation "io.reactivex.rxjava2:rxjava:$rxjava_version"
ext.rxjava_version = "2.2.12"
ext.rxkotlin_version = "2.4.0"
ext.rxandroid_version = "2.1.1"
ext.rxjava_adapter_version = "2.4.0"
ext.room_version = '2.2.0-rc01'
Однако я могу заставить работать журнал и тост.