Auto Logout from app after 15 min due to inactivity in android -
i want session management in android application.i want logout user application if inactive or not interacting application. not sure when start service. starting in onresume(). have used countdowntimer automatic logout after 15min. not working. there efficient or working solution 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 service"); // code 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(); }
first of 1 reason can guess why not working may because of code.
@override protected void onstop() { super.onstop(); logoutservice.timer.cancel(); }
lets assume if device screen goes of onstop() triggered hence timer cancelled , no further checking.
cancel time on ondestroy this.
@override protected void ondestroy() { super.ondestroy(); logoutservice.timer.cancel(); }
now since ask there better way of doing can use touch event
and start counter in thread or timer , if counter reaches 15 min close activity calling
finish()
here code 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 reset counter ontouch return true; } });
Comments
Post a Comment