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.

Here are the best offer and recommendation for you to download this. 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.

Leave a Reply

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