Friday, 2 October 2015

Auto logout after 15 minutes due to inactivity in android

create a service


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

           public void onFinish() {
               Log.v(TAG, "Call Logout by Service");
               // Code for Logout
               stopSelf();
           }
        };
}



@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

}


then add the code for all activity

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    try {
    LogoutService.timer.start();
} catch (Exception e) {
e.printStackTrace();
}
   
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    LogoutService.timer.cancel();
}   


here after certain minutes the first page will be displayed automatically


1 comment:

  1. Here CountDownTimer is null oject reference .please help me

    ReplyDelete