Issue
I'm making an app in kotlin that query an API once a day with alarm manager.
Now I thought of a problem: what to do if there is no internet connection when the alarm manager is executed?
My best idea is to query the API and if there is no response I would set another alarm manager to half an hour later.
the problem is that the alarm manager needs a pending intent and I don't know how to call another function in the activity with a pending intent.
I can just call the same activity with another intent, but I'm afraid that if there won't be internet connection several times in a row something bad will happen because there will be to many intents.
So my question is if the bad thing will actually happen because of all the intents
or maybe you have a better solution than mine...
Solution
You should use WorkManager
for this purpose. It is designed to support these kinds of tasks. You can schedule periodic execution and tell WorkManager
that your task needs Internet connectivity. WorkManager
ensures that the task only runs when there is connectivity. Tasks scheduled with WorkManager
are persisted across device restarts so you don't have to worry about rescheduling them after a device reboot.
See https://developer.android.com/topic/libraries/architecture/workmanager
Answered By - David Wasser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.