What's the best approach to save data to room when app is completely closed at a specific time in Android

Viewed 28

I have a simple Android app I have been working on for a few weeks.

The app is a diary to which you can set tasks you have to complete.

I need the app to have a functionality in which it will save an object to room at 12:00 am while the app is either open, open in the background, or completely closed.

My question to you is what do you think would be the best and most efficient approach for building this feature?

Thank you

1 Answers

I think the best fit for this might be WorkManager: https://developer.android.com/topic/libraries/architecture/workmanager

You are basically abstracting out most of the other obvious choices, like AlarmManager, Services (background/foreground) - WorkManager lets the OS pick the best approach for your device and you only have to define it once.

Scheduling for specific times like you want is covered here: https://developer.android.com/topic/libraries/architecture/workmanager/how-to/define-work#schedule_periodic_work

Related