I’m looking to implement a feature that logs out the user after X minutes of inactivity. After going through a similar answer on SO, the suggested method seems to be to –
- Have a timer running in the background. Schedule it to timeout after x minutes.
- In every function where the user interacts with the app (basically all event handlers), call a method that resets the timer.
I can’t think of anything better than this myself, but it’s seems to be a huge pain even for a medium sized application that has 6-7 different screens and a whole bunch of UI components. Is there a better way to handle this?
Thanks,
Teja.
No and yes. Use a timer if you’re implementing it in a
Serviceor in anIntentService. Otherwise, don’t.That solution would be hard to maintain.
You should have an
IntentService(demonstrating article here) running in the background that can easily implement aTimerTaskor aHandlerand make the runnable code inside it fire a broadcast to your activities. In your activities you can easily add aBroadcastRecieverand in that case you can log out the user if time is out. You can start your service when your application isn’t visible to the user.