I am working on news reader application and now i want to achieve this: Once a day when there is Internet connection or at a specific time for example at 23.00 my app will fetch all new news and then store it in db. Inside application I am using retrofit to fetch and room to store using mvvm pattern. How can I do this? Any suggestiions are appreciated.
2 Answers
You can achieve this by using flex period in PeriodicWorkRequest.Builder. As example, if you want run your Worker within 15 minutes at the end of repeat interval (let's say 8 hours), code will look like this:
val periodicRefreshRequest = PeriodicWorkRequest.Builder(
RefreshWorker::class.java, // Your worker class
8, // repeating interval
TimeUnit.HOURS,
15, // flex interval - worker will run somewhen within this period of time, but at the end of repeating interval
TimeUnit.MINUTES
)

- 1,965
- 1
- 16
- 43
You can do this via Service in android it has below features -
Foreground
A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app.
Background
A background service performs an operation that isn't directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.
You can read more about this in below link -

- 1,932
- 2
- 12
- 24
-
Service is not a good approach because you have to run it all the time best solution is Content Provider /Content Resolver https://stackoverflow.com/a/40562442/2692102 – Hanzala Feb 11 '20 at 08:10
-
Why we have to run all the time without need whenever you work is finished you can stop the service it's simple as that. – Dipankar Baghel Feb 11 '20 at 08:17
-
@Hanzala can I achieve everything what I want using content Provider ?? – Whiteq Feb 11 '20 at 08:28
-
explain about **eveything** – Hanzala Feb 11 '20 at 09:41
-
@DipankarBaghel what if device is restarted and service is not started again and we are willing to do some thing with that? and how service will receive data from any other application? – Hanzala Feb 11 '20 at 09:44
-
@Hanzala If device restarted then there is is a provision is service to restart it again with redelivering the previous Intent. I think you need to read about START_REDELIVER_INTENT, START_NOT_STICKY and START_STICKY. Hope this will clear your doubt. – Dipankar Baghel Feb 11 '20 at 09:48
-
1Use Work manager or Job Scheduler for this task.Run a service every time in background is bad approach. https://stackoverflow.com/questions/50357066/periodic-daily-work-requests-using-workmanager – Muhammad Usman Butt Feb 11 '20 at 09:57
-
@White check my Answer – Hanzala Feb 11 '20 at 09:59
-
https://developer.android.com/about/versions/oreo/background#services check this. – Hanzala Feb 11 '20 at 10:06
-
@MuhammadUsmanButt will you please explain running a service in background is a bad approach. where is this written? this will be helpful for me. – Dipankar Baghel Feb 11 '20 at 10:06
-
@Hanzala I didn't find anywhere that state's that service is a bad approach for background task. It recommends only to use updated api's and migration of service only for better use experience that's it. – Dipankar Baghel Feb 11 '20 at 10:12
-
@DipankarBaghel Services running in the background can consume device resources, potentially resulting in a worse user experience. this is not my line l copy paste this line from https://developer.android.com/about/versions/oreo/background – Muhammad Usman Butt Feb 11 '20 at 10:12
-
@Hanzala I will check it – Whiteq Feb 11 '20 at 10:51