I'm designing an app that has a recurring task of sending presence to a dedicated server as long as the app is in foreground.
In my searches across the web I saw a few different approaches and wanted to know what is the best way of doing this.
What is the best way to schedule a server call?
The options I saw were:
Timer .
BroadcastReciever with AlarmManager.
What's your opinion?
EDIT:
The reason I need this is for a chat based app that sends all the user actions to a remote server.
i.e. user is typing a message, user is reading a message, user is online, user is offline etc.
This means that once every interval, I need to send the server what I'm doing, since I open a chat room with other people, they need to know what I'm doing.
Similar to the whatsapp message feedback mechanism:
EDIT #2:
Recurring tasks should now be scheduled almost always via the JobScheduler
API (or FirebaseJobDispatcher
for lower APIs) in order to prevent battery draining issues as can be read in the vitals section of the Android training
EDIT #3:
FirebaseJobDispatcher has been deprecated and replaced by Workmanager, which also incorporates features of JobScheduler.