-2

I want to do session management in my android application.I want to logout the user from the application if they are inactive or not interacting with the application. I am not sure when to start the service. Currently I am starting it in onResume(). I have used CountDownTimer to do automatic logout after 15min. But this is not working. Is there any efficient or working solution for session management.

LogoutService:

public class LogoutService extends Service {
    public static CountDownTimer timer;
    @Override
    public void onCreate(){
        // TODO Auto-generated method stub
        super.onCreate();
        timer = new CountDownTimer(5 * 60 * 1000, 1000) {
            public void onTick(long millisUntilFinished) {
                //Some code
                Log.v("LogoutService", "Service Started");
            }

            public void onFinish() {
                Log.v("LogoutService", "Call Logout by Service");
                // Code for Logout
                stopSelf();
            }
        };
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

MainActivity:

@Override
        protected void onStop() {
            super.onStop();
            LogoutService.timer.cancel();
        }

    @Override
        protected void onResume() {
            super.onResume();
            startService(new Intent(this, LogoutService.class));
            LogoutService.timer.start();
        }
sagar suri
  • 4,351
  • 12
  • 59
  • 122
  • Possible duplicate: https://stackoverflow.com/questions/8968265/android-auto-logout-when-app-goes-to-background?rq=1 – Kuffs Sep 12 '17 at 06:25
  • Why didn't you try searching ? [here](https://stackoverflow.com/questions/4208730/how-to-detect-user-inactivity-in-android) is what you want. – Somesh Kumar Sep 12 '17 at 06:26
  • @SomeshKumar I tried that solution. But didnt workout for 15min. its working if the screen in active. If the screen turns off it doesn't workout. – sagar suri Sep 12 '17 at 06:34
  • The implementation is for immediate logout if the application goes in background @Kuffs – sagar suri Sep 12 '17 at 06:47
  • The question may have suggested that but the answers covered various options. Did you read them? My answer in-particular covers your use case. – Kuffs Sep 12 '17 at 06:54
  • Yeah went through it. But problem comes when I need to check the inactivity till 15min. If the user starts using the application within 15min then i need to reset the `CountdownTimer` and check for the inactivity. – sagar suri Sep 12 '17 at 06:56
  • My answer does not have or need a countdowntimer and services can be closed by the system preventing yours from ever finishing. Android 8 especially is very strict about background services. – Kuffs Sep 12 '17 at 06:57
  • @Kuffs with all due respect. I am not able to that solution to fit my need. Can you integrate that answer with my need. That will be more helpful. – sagar suri Sep 12 '17 at 06:59
  • No I cannot integrate my answer with using a countdowntimer within a service. As I said, services can be killed. My answer does not need timers or services. It just logs out. On Android 8 though, use a JobScheduler instead of an AlarmManager. – Kuffs Sep 12 '17 at 07:04
  • can you put your solution as an answer please @Kuffs – sagar suri Sep 12 '17 at 07:06

2 Answers2

0

first of all one reason I can guess why its not working is may because of this code.

 @Override
    protected void onStop() {
        super.onStop();
        LogoutService.timer.cancel();
    }

Lets assume if device screen goes of onStop() will triggered hence your timer is cancelled and no further checking.

cancel the time on onDestroy like this.

 @Override
    protected void onDestroy() {
        super.onDestroy();
        LogoutService.timer.cancel();
    }

Now since you ask is there any better way of doing is you can use the touch event

and start a counter in Thread or Timer and If counter reaches to 15 min close your activity by calling

 finish()

Here is code to detect onTouch event of entire activity onCreate()

   setContentView(R.id.main);
   View view = findViewById(R.id.main); 
    view.setOnTouchListener(new View.OnTouchListener() {

         @Override
          public boolean onTouch(View view,MotionEvent event) {

               nTouchCouter = 0; // Don't forget to reset the counter onTouch
               return true;

                       }
             });
Tejas
  • 358
  • 5
  • 23
  • can you provide with a complete implementation ? I am getting bugged with this since a week. – sagar suri Sep 12 '17 at 06:38
  • @hiren did you try first moving your timer.cancel to onDestroy()?? – Tejas Sep 12 '17 at 06:39
  • yeah. I have tried now and its working. But its logging me out after 5mins no matter what. I guess I need to integrate it with `onTouchListener()`. How can it be done ? – sagar suri Sep 12 '17 at 06:43
  • because your timer is set to five minutes :-) 5 * 60 * 1000 change it to 15*60*1000 yallla done.. – Tejas Sep 12 '17 at 06:44
  • What if the user start using my application in the 14th min. Its logging me out still. How can I stop that ? Because CountdownTimer is not getting stopped after the user start using the application again. – sagar suri Sep 12 '17 at 06:46
  • yeah gottcha.. I have added touchlistener code in my Answer thats should be done in oncreate() of activity – Tejas Sep 12 '17 at 06:51
  • can you provide with the implementation ? – sagar suri Sep 12 '17 at 06:52
0

This have worked for me:

private Handler handler;
private Runnable r;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       // Your code

 handler = new Handler();
        r = new Runnable() {
            Toast.makeText(MainActivity.this, "user is inactive from last 5 minutes", 
     Toast.LENGTH_SHORT).show();
           // Preform you action here
        };
        startHandler();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    stopHandler();
}

@Override
public void onUserInteraction() {
    super.onUserInteraction();
    stopHandler();
    startHandler();
}

public void stopHandler() {
    handler.removeCallbacks(r);
}

public void startHandler() {
    handler.postDelayed(r, 10000);
}