Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto logout after X minutes, Android

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.

like image 970
Tejaswi Yerukalapudi Avatar asked Apr 18 '11 15:04

Tejaswi Yerukalapudi


2 Answers

You can use a CountDownTimer and restart it from onUserInteraction() in every Activity()

like image 191
Umek Avatar answered Sep 29 '22 11:09

Umek


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.

like image 25
Wroclai Avatar answered Sep 29 '22 11:09

Wroclai