I implemented an AlarmManager
to send notifications when user adds a due date to a Task. However, when the user turns off the device, all the alarms are lost. Now I'm updating the BroadcastReceiver
to receive an android.intent.action.BOOT_COMPLETED
and reschedule all the alarms set to each task.
My first attempt was to get an Rx Single
with all the tasks where the due date is higher than the current time inside the BroadcastReceiver
, then reschedule all the alarms. The issue is I'm not able to dispose the Observable
once the BroadcastReceiver
has no lifecycle. Also, it seems that this is not a good approach.
During my researches, the IntentService
was a good solution for this case, but I'm getting into the new WorkManager
library and the OneTimeWorkRequest
looks like a good and simple solution.
The Worker
is being called and executing correctly, but I'm not able to dispose the Observable
because the onStopped
method is never called.
Here is the implementation, based on this snippet:
class TaskAlarmWorker(context: Context, params: WorkerParameters) :
Worker(context, params), KoinComponent {
private val daoRepository: DaoRepository by inject()
private val compositeDisposable = CompositeDisposable()
override fun doWork(): Result {
Timber.d("doWork")
val result = LinkedBlockingQueue<Result>()
val disposable =
daoRepository.getTaskDao().getAllTasks().applySchedulers().subscribe(
{ result.put(Result.SUCCESS) },
{ result.put(Result.FAILURE) }
)
compositeDisposable.add(disposable)
return try {
result.take()
} catch (e: InterruptedException) {
Result.RETRY
}
}
override fun onStopped(cancelled: Boolean) {
Timber.d("onStopped")
compositeDisposable.clear()
}
}
- Is
WorkManager
a good solution for this case? - Is it possible to dispose the
Observable
correctly?