I am trying to implement an auto logout functionality. If the application is not used for a period of 1 hour i would want to automatically bring the user to the login screen. When the phone gets locked the thread which is monitoring if the application is in use does not seem to continue. My code is as below:
/**
* This Deamon checks if the application is idle and
*/
private class LogoutDeamon extends Thread {
public void run() {
while (!logoutDeamon) {
try {
System.out.println("Logout Counter:" + logoutConter);
if (logoutConter <= 0) {
logoutDeamon = true;
ApplicationManager.getInstance().setLoggedOut(true);
ApplicationManager.getInstance().Log(Level.INFO, "Auto Log out");
logout();
} else {
decreamentCounter();
}
sleep(60000 * 1);
} catch (ParserException ex) {
Log(Level.ERROR, " Par. Ex. in Logout-Deamon:" + ex.getMessage());
logout();
} catch (ServerException ex) {
Log(Level.ERROR, " Ser. Ex. in Logout-Deamon:" + ex.getErrorMessage());
logout();
} catch (InterruptedException ie) {
Log(Level.ERROR, "Int. Ex. in Logout-Deamon:" + ie.getMessage());
} catch (Exception ex) {
Log(Level.ERROR, "Erro in Logout-Deamon:" + ex.getMessage());
logout();
}
}
}
private void logout(){
Intent broadcastIntent = new Intent();
broadcastIntent
.setAction("com.package.ACTION_LOGOUT");
applicationContext.sendBroadcast(broadcastIntent);
Intent loginIntent = new Intent(applicationContext,
Login.class);
loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
applicationContext.startActivity(loginIntent);
}
}
Am i missing out on something? Can someone kindly help me with this. Thanks in advance.
I don’t think that you need a thread. Register the time in onPause. In onResume test if 1 hour has passed. If not, reset the timer.