Ideas are easy. Implementation is hard. Be Passionate.

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

publicclassApplockManager{

privatestaticApplockManager instance;

privateDefaultApplock currentAppLocker;

publicstaticApplockManager getInstance(){

if(instance==null){

instance=newApplockManager();

}

returninstance;

}

publicvoidenableDefaultAppLockIfAvailable(Application currentApp){

currentAppLocker=newDefaultApplock(currentApp);

}

publicvoidupdateTouch(){

currentAppLocker.updateTouch();

}

}

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

publicclassWaiterextendsThread

{

privatestaticfinalStringTAG=Waiter.class.getName();

privatelonglastUsed;

privatelongperiod;

privatebooleanstop=false;

privateContext mContext;

publicWaiter(Context context,longperiod){

this.period=period;

stop=false;

mContext=context;

}

publicvoidrun(){

longidle=0;

this.touch();

Log.d("Value of stop",String.valueOf(stop));

do

{

idle=System.currentTimeMillis()-lastUsed;

// Log.d(TAG, "Application is idle for "+idle +" ms");

try

{

Thread.sleep(2000);//check every 5 seconds

}

catch(InterruptedExceptione)

{

Log.d(TAG,"Waiter interrupted!");

}

if(idle&gt;period)

{

idle=0;

//do something here - e.g. call popup or so

// Perform Your desired Function like Logout or expire the session for the app.

stopThread();

}

}

while(!stop);

Log.d(TAG,"Finishing Waiter thread");

}

publicsynchronized voidtouch(){

lastUsed=System.currentTimeMillis();

}

publicsynchronized voidforceInterrupt(){

this.interrupt();

}

publicsynchronized voidsetPeriod(longperiod)

{

this.period=period;

}

publicsynchronized voidstopThread(){

stop=true;

}

publicsynchronized voidstartThread(){

stop=false;

}

publicsynchronized voidcloseThread(){

// Perform Your desired Function like Logout or expire the session for the app.

stopThread();

}

}

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().

1

2

3

4

5

6

7

8

9

10

publicABFSApplication getApp(){

return(ABFSApplication)this.getApplication();

}

@Override

publicvoidonUserInteraction(){

super.onUserInteraction();

getApp().touch();

Log.d("TAG","User interaction to "+this.toString());

}

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

1

2

3

publicvoidtouch(){

ApplockManager.getInstance().updateTouch();

}

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.

Categories

For better assist

Hey Folks, Welcome to the MobologicPlus blog. I’m Sunil Gupta, a tech blogger from Bangalore, India.
I started MobologicPlus as a passion, by helping people to understand the technical kinds of stuff from this blog. You have some feedback for MobologicPlus. Where should I send it? Sure, drop us a line anytime at contact@mobologicplus.com