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();
}