【发布时间】:2014-09-04 04:30:02
【问题描述】:
我正在编写一个需要在一段时间不活动后自动注销的应用程序。
我使用计时器在一段时间不活动后触发注销事件。
- 在触发注销事件后,用户将进入登录活动。
- 当应用处于后台或前台时,可以触发注销事件。
- (已添加) 如果用户打开了另一个应用程序,则该应用程序不会重新打开。仅当用户查看主屏幕 [桌面] 时它才会重新打开
问题:如果应用程序在后台,登录活动的启动会重新打开应用程序。
LogoutTimer.java
负责解雇
new LogoutEvent() 在 AUTO_LOGOUT_WAIT_TIME_MS
public class LogoutTimer {
private static LogoutTimer timer;
private Handler handler;
private Runnable runnable;
private LogoutTimer() {
this.handler = new Handler();
this.runnable = new Runnable() {
@Override
public void run() {
Log.i(AppConfig.LogTags.SECURITY, "auto logout of application");
EventBus.getDefault().post(new LogoutEvent(LogoutTypeEnum.SESSION_EXPIRED));
}
};
}
public static LogoutTimer getInstance() {
if (timer == null) {
timer = new LogoutTimer();
}
return timer;
}
public void restart(String source) {
Log.i(AppConfig.LogTags.SECURITY, "restarting logout timer. Source: (" + source + ")");
this.clearHandler();
handler.postDelayed(runnable, AppConfig.AUTO_LOGOUT_WAIT_TIME_MS);
}
public void stop(String source) {
Log.i(AppConfig.LogTags.SECURITY, "stopping logout timer. Source: (" + source + ")");
this.clearHandler();
}
private void clearHandler() {
handler.removeCallbacks(runnable);
}
}
MyApplication.java(事件监听器)
以下代码启动我的 LoginActivity。它还重新打开应用程序甚至在后台时!
当我将此代码放入 Activity 时也会出现此问题。
public class MyApplication extends Application {
// other non relevant code here
public void onEvent(LogoutEvent event) {
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(AppConfig.Args.DATA_ITEM_OTHER, event.getLogoutType());
startActivity(intent);
}
}
【问题讨论】:
-
发布您的登录活动.....当您的注销活动关闭时,您的应用程序不会,所以它总是会重新打开它..lol..实际上我现在正要阅读您的问题..lol
-
最好使用后台服务,而且对电池也很友好!
-
@Elltz 正确。但是,我不确定为什么应用程序在后台运行后又回到前台。我希望应用程序无法自行重新打开。
-
“重新打开应用程序”是什么意思?只要其中一个活动正在运行,应用程序对象就会处于活动状态
-
loginActivity 来到前台(弹出)
标签: java android android-intent