Periodic HTTP requests in Android foreground service

Viewed 181

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?

0 Answers
Related