User start my app and logs in.
Selects Session Timeout to be 5 mins.
Does some operations on the app. (all in foreground)
Now User bring Myapp to background and starts some other app.
----> Count down timer starts and logs out user after 5 mins
OR user turns the screen OFF.
----> Count down timer starts and logs out user after 5 mins
I want the same behavior even when the app is in the foreground but user doesn't interact with the app for a long-time say 6-7 mins. Assume the screen is ON all the time. I want to detect kind of user inactivity (No interaction with app even though the app is in the foreground) and kick start my count down timer.
User inactivity can detect using
onUserInteraction()
override method in androidHere is the sample code, signout (HomeActivity-->LoginActivity) after 3min when user inactive
isInForeGrnd ===> logic is not shown here as it is out of scope of the question
You can wake the lock to the cpu by using the device code below-
I think you should go with this code, this is for 5min idle session timeout:->
In my activity base class I created protected class:
So in onResume method - you can specify action in your callback what do you wish to do with it...
Even you can manage your requirement with @gfrigon or @AKh solutions.
But I was thinking about some Timer and Handlers free solutions for this. I already have well managed Timer solution for this. But i have successfully implemented Timer and Handler free solution.
First i tell you what you have to manage if you use Timer or Handlers.
Finally i implemented a solution which is
ACTION_SCREEN_ON
/ACTION_SCREEN_OFF
Broadcast Receiver.Solution
We will not observe user inactivity by timers rather than we will check last activity time on user activity. So when user interact app next time, i check last interaction time.
Here is
BaseActivity.class
which you will extend from every of your activity class instead ofLoginActivity
. You will define your logout time in fieldTIMEOUT_IN_MILLI
in this class.