Reputation: 1038
This question is more about what tools to use not about exact code.
I have an android app that allows a user to define some tasks. Each task is related to periodical REST HTTP queries - let's say every 5 minutes. And should be continued even if user closes the app. Information about progress is displayed using foreground service. When task finishes - fails or succeeds - a notification is sent.
Now my solutions is like this:
When application starts I bind to a service. When user adds a task I send it to the service and the service starts foreground service. The task is run in a thread in infinite loop: send request -> sleep -> send request -> sleep...
When task finishes I send broadcast notification. And if all tasks are done then I stop foreground service (more or less).
It seems to work but my feeling is that I'm not using the right tools:).
Is there a better way to run such a periodic task? What if I'd like to change frequency in the meantime?
Should I use broadcast to send notification from service to application? I used listeners but it seems that there is no reliable way to deregister a listener when application is closed by "system".
Finally: Is foreground service a good place to do all the things? Or maybe it should only be responsible for displaying notifications?
Upvotes: 1
Views: 592
Reputation: 13
Did you end up getting something else that worked? I'ved tried it with the workmanager, but that has restrictions on foreground notifications that I have not been able to get around.
Though if your work is < 10 minutes, then the job still runs to completion, so that might be a solution for you.
Upvotes: 0