Auto logout after X minutes, Android

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


This is how I would go about this:

1) Create a global variable to represent a time log

2) During the onStop call for each activity, update the global variable with the current time.

3) During the onResume call for each activity, compare the current time with the global variable time to see how much time has passed


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.