关闭应用程序在后台意图启动后重新打开

时间:2014-09-04 04:30:03

标签: java android android-intent

我正在编写一个需要在一段时间不活动后自动注销的应用程序。

我使用计时器在一段时间不活动后触发注销事件。

  • 触发注销事件后,用户将进入登录活动。
  • 当应用在后台或前台时,可以触发注销事件。
  • (已添加)如果用户已打开其他应用,则该应用无法重新开启。它仅在用户查看主屏幕[桌面]
  • 时重新打开

问题:如果应用程序在后台,则登录活动的开始会重新打开该应用程序。

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);
    }
}

2 个答案:

答案 0 :(得分:0)

好的..这就是我的想法..它自动注销然后带你到loginactivity很好 - 如果用户在您的应用程序ryt上闲置? 另一个条件是,如果用户当前不在您的应用程序上,它不应该启动ryt ??

我也会为您创建静态自定义数据和布尔值,以便存储您的条件,因此可能是时间到期,因为您的应用程序在后台运行 你可以存储一个

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,

就会触发logout为true。在if(){}语句中,检查主活动的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;
        }
    };
}

希望我足够清醒。我知道这是否有用或其他什么大声笑..

答案 1 :(得分:0)

感谢您对@Elltz的帮助。我从answer中获取的是:

  1. 我不应该在应用程序未处于活动状态时触发loginActivity(这似乎是导致应用程序返回到前台的原因)
  2. 我可以使用标记来跟踪应用程序是否“暂停”
  3. 我是如何做到这一点的:

    BaseActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.application = (MyApplication) getApplication();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        this.application.activityPaused();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        this.application.activityResumed();
    }
    

    MyApplication.java

    private void startLoginActivity(LogoutTypeEnum logoutType) {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(AppConfig.Args.DATA_ITEM_OTHER, logoutType);
        startActivity(intent);
    }
    
    public void onEvent(LogoutEvent event) {
        if (this.isActivityPaused) {
            this.shouldLogoutOnResume = true;
            return;
        }
    
        this.startLoginActivity(event.getLogoutType());
    }
    
    public void activityPaused() {
        this.isActivityPaused = true;
    }
    
    public void activityResumed() {
        this.isActivityPaused = false;
    
        if (this.shouldLogoutOnResume) {
            this.startLoginActivity(LogoutTypeEnum.SESSION_EXPIRED);
            this.shouldLogoutOnResume = false;
        }
    
    }
    
相关问题