我正在编写一个应用程序,需要在一段时间不活动后自动注销。
我使用计时器在一段时间不活动后触发注销事件。
- 触发注销事件后,用户将进入登录 Activity 。
- 当应用处于后台或前台时,可以触发注销事件。
- (已添加)如果用户打开了另一个应用程序,该应用程序不会重新打开。仅当用户查看主屏幕 [桌面] 时才重新打开
问题:如果应用程序位于后台,则登录 Activity 的启动将重新打开该应用程序。
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);
}
}
最佳答案
好的..这就是我的想法..它会自动注销,然后带您进入登录 Activity ,很棒 - 仅当用户在您的应用程序上闲置时? 另一个条件是,如果用户当前不在您的应用程序上,则它不应该启动 ryt??
我还建议您创建静态自定义数据和 boolean 值,以便存储您的条件,所以也许时间到了,因为您的应用程序在后台运行 你可以存储一个
boolean logout = true;
所以下次用户回来时,它会检查状态并从那里继续..您也可以存储您的数据..放置一些代码让我们看看
public class MyApplication extends Application {
// other non relevant code here
static boolean activityOnpauseCalled, logOut = false; //put activityOnpauseCalled = true in your onpause of the main activity
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());
if(!MyApplication.activityOnpauseCalled){
startActivity(intent);
}
}
}
只要触发注销事件并且 ActivityOnPauseCalled 为 true,就会将注销触发为 true。在 if(){} 语句中并检查主 Activity 的 onresume 上的 logOut 是否为 true
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
MyApplication.activityOnPauseCalled = true;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(MyApplication.logOut){
MyApplication.logOut = false;
//let the user sign in
}else{
//do otherwise.. think that would not happen in your case right?? btw,let it start normally
}
}
最后是定时器任务
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));
MyApplication.logOut = true;
}
};
}
希望我足够清醒..让我知道这是否有帮助或其他什么哈哈..
关于java - 关闭的应用程序在后台 Intent 启动后重新打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25657321/