Auto Logout or Expire the session after 5 minutes of user inactivity

Sometimes we need to log out the user or expire his session if he did not perform any interaction with the app for 5 minutes or even If he locks the screen from the lock button. This can be implemented very easily by maintaining a thread(Waiter) which will give alert once 5 minutes are over after last user interaction.

First, create a file AppLockManager like this.

Then create a file DefaultApplock by extending ActivityLifeCycleCallbacks this will provides with lifecycle methods like onActivityResume and onActivityPause etc.

Now Create a Waiter thread which will keep a record of the time that is elapsed after the last user interaction and will provide an alert once that time exceeds 5 minutes.

Now in your base activity Override UserInteraction Method to update the timer for the waiter thread. In this method we will start the timer every time user perform any interaction with the app. getApp() function will be used to get applicationContext().

Now lastly in your application file write this method touch to update userInteraction.

Please do subscribe email to get all newsletters of this blog and if you feel that this post will help you to understand then do not forget to subscribe, share and comment below.

I am a very enthusiastic Android developer who develops applications for devices powered by the Android operating system. I keep myself updated with the latest technologies available in Android.

One thought on “Auto Logout or Expire the session after 5 minutes of user inactivity

  1. Hi Abhishek,

    will this work if my app is in kiosk mode – basically app will be the launcher and there will never be screen lock happening unless done purposely. My expectation is if user doesn’t have any event called- click or touch in app for 5 mins than user should be logged out . please help.

Leave a Reply

Your email address will not be published. Required fields are marked *