2

I want to logout users after they have been inactive for 3 minutes on my app i.e doing nothing for 3 minutes. I have only one Activity and it contains multiple Fragments. How can I achieve this? I tried the answers posted on the same topic but nothing worked. Please can anyone be specific?

Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60

4 Answers4

2

Each Activity has an onUserInteraction() function. You can override it and reset a Runnable. If there is no user interaction the Runnable will be called after 3 minutes. Don't forget to remove Runnable onDestory().

Runnable r = new Runnable() {

    @Override
    public void run() {
        // handle timeout
    }
};

Handler handler = new Handler();

@Override
public void onUserInteraction() {
    super.onUserInteraction();
    handler.removeCallbacks(r);
    handler.postDelayed(r, 3 * 60 * 1000 );
}

@Override
protected void onDestroy() {
    super.onDestroy();
    handler.removeCallbacks(r);
}
Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
0

Try this

Use CountDownTimer

CountDownTimer timer = new CountDownTimer(15 *60 * 1000, 1000) {

        public void onTick(long millisUntilFinished) {
           //Some code
        }

        public void onFinish() {
           //Logout
        }
     };

When user has stopped any action use timer.start() and when user does the action do timer.cancel()

Kristofer
  • 809
  • 9
  • 24
0

use alarm manager and set alarm for logout

Pankaj K Sharma
  • 240
  • 1
  • 12
0

You have two options.

  1. Use an alarm manager and have a broadcast receiver upon receiving the alarm and log out from there. When the user does some activity, restart the alarm. example.
  2. have a Service that you can talk to, and send intents restartTimer. When the timer reaches zero, log out the user.
Adib Faramarzi
  • 3,798
  • 3
  • 29
  • 44