-3

I am developing an Android application in which I can login with a particular userid and password. I want my application to be able to automatically log the user out when the application state is idle for 5 minutes.

How can I achieve this in Android.

midhunhk
  • 5,560
  • 7
  • 52
  • 83
madan V
  • 844
  • 3
  • 19
  • 40

2 Answers2

1

Use AlarmManager

Basic idea is, you override onPause() set an alarm in it for 5 minutes, and when that alarm returns, "log-out" the user. If the user returns you can cancel that alarm (override onResume()

Here is a good thread explaining usage of AlarmManager with code for cancelling and scheduling new alarms as well as handling the callbacks.

If you want to do it simpler, you can keep track of a static timestamp variable. Update this variable in your onPause(), then when the user returns (onResume()) check the time of this variable, see if 5 minutes has passed, if it has redirect user back to login

Community
  • 1
  • 1
Ahmed Aeon Axan
  • 2,139
  • 1
  • 17
  • 30
0

Depending on your definition of the idle state, you can start a Service when your activity is paused, where a Timer will run for 5 minutes before logging out the user and stopping the service. In each Activitiy you stop the timer in onResume() and start it again in onPause(). You can implement this behavior easily with a common base Activity. This will log out the user 5 minutes after he exited the last Activity in you application.

Thrakbad
  • 2,728
  • 3
  • 23
  • 31