I'm trying to implement a timeout mechanism for a user's login session in my app ie. If user has not interacted with my app for say 10 mins then when he uses the app next (regardless of whether the app was previously in the background or currently in the foreground) he should be taken to the login screen.
My current implementation uses:
- a singleton instance of a Java Thread (call it 'TimeoutThread') from my 'BaseActivity' (all my activities except the LoginActivity extend BaseActivity)
- so the singleton 'TimeoutThread' will be started only the first time any post-login activity starts up.
- 'TimeoutThread' will simply keep track of the 'lastUserInteractionTimeStamp' inside a while() loop with a Thread.wait()...
so it wakes up every 2 secs, checks if the (currentTime-lastUserInteractionTimeStamp) is greater than say 10 mins and if yes then it'll trigger some callback if callback object is not null (the callback is made null whenever the app goes into background, etc)
This method works fine right now.
My concern is that since this thread wakes up every few secs (Thread.wait() inside a while(true) loop) even when the app goes into the background (eg. user presses Home button), it will cause a battery drain.
I've been unable to find a good link to explain the effects of keeping such a thread.
I did find links for power optimization, scheduling recurring tasks etc (pasted below) but couldn't directly find my answer there.
https://developer.android.com/training/monitoring-device-state/index.html
Scheduling recurring task in Android
Can anyone critique this approach / suggest a better one ?