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 -
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.
You can use a CountDownTimer
and restart it from onUserInteraction()
in every Activity()
Have a timer running in the background. Schedule it to timeout after x minutes.
No and yes. Use a timer if you're implementing it in a Service
or in an IntentService
. Otherwise, don't.
In every function where the user interacts with the app (basically all event handlers), call a method that resets the timer.
That solution would be hard to maintain.
You should have an IntentService
(demonstrating article here) running in the background that can easily implement a TimerTask
or a Handler
and make the runnable code inside it fire a broadcast to your activities. In your activities you can easily add a BroadcastReciever
and 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With