我正在尝试实现自动注销功能。如果应用程序未使用1小时,我会自动将用户带到登录屏幕。当手机被锁定时,正在监视应用程序正在使用的线程似乎不会继续。我的代码如下:
/**
* 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);
}
}
我错过了什么吗?有人可以帮助我这个。提前谢谢。
答案 0 :(得分:1)
我认为你不需要一个帖子。在onPause中注册时间。在onResume测试,如果已经过了1小时。如果没有,请重置计时器。