I am implementing a session manager for my application. Each time an activity is resumed, it checks if the session is active. If it is, the activty starts, else it displays another activity authenticating the user. Whenever an activity is paused, it sets the current time in the session manager. The session is active when there is a running activity, or the last timestamp set in pause is no older than x minutes.
The problem with this is that if the user changes time settings, it is possible that the session looks active again, due to the timestamp part.
I want to solve this with a service that is started when the date and time setting is changed (I know there are intents describing this event) and resets the session. It only needs to run if the application is still running, because the session manager will be a static class which loses state when the process is finished.
How can I create shuch a service?
You should not need a service for this. You should be able to implement a
BroadcastReceiverin the manifest forACTION_TIME_CHANGED(and possiblyACTION_TIMEZONE_CHANGED, as I am uncertain if that will affect your session). InonReceive(), check and see if you have a session. If you do not, then the user changed the time when your application was not running, and you are OK. If you do have a session, then you can take whatever steps you want at this point (e.g., invalidate the session).