Search code examples
javaandroidalarmmanager

send data to server every minute (For check status user)


For check status user i should send some data to server every 5 minutes with retrofit2.

Of course, this information should be sent when the user is in the app

This means that when the user is out of the application, there is no need to send something to the server

What is the best way to do this?

For connection to server i use retrofit2.


Solution

  • One solution is to use Android's work manager to ping your server with http request. You can create initial OneTimeWorkRequest when user logins and with in each execution create a new OneTimeWorkRequest with 5 minutes delay. When user exits the app cancel all the work request.

    See the Jetpack Library: https://developer.android.com/topic/libraries/architecture/workmanager

    Another solution for which you need to make changes in your architecture is to use Sockets with library like SocketIO. Connect the socket from your app when user login notifying server that user is online. When socket disconnect server can mark user offline: https://socket.io/blog/native-socket-io-and-android/

    One other solution is to use Firebase Realtime database for presence system. Firebase has nice example of how to do this: https://firebase.googleblog.com/2013/06/how-to-build-presence-system.html