【问题标题】:Closed app reopens itself after background intent launch后台意图启动后关闭的应用程序自行重新打开
【发布时间】: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


【解决方案1】:

好的..这就是我的想法..它会自动注销然后将您带到 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 为真,就将注销触发为真。在 if(){} 语句中检查 logOut 在主 Activity 的 onresume 上是否为真

@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
    }
}

最后是timertask

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

希望我足够清醒..让我知道它是否有帮助或其他什么哈哈..

【讨论】:

  • 感谢您付出了这么多努力。这个解决方案看起来很有希望。明天我回到办公室时会试一试
【解决方案2】:

感谢您对@Elltz 的帮助。我从你的answer 那里得到的是:

  1. 我不应该在应用程序未激活时触发 loginActivity(似乎这是导致应用程序返回前台的原因)
  2. 我可以使用标志来跟踪应用程序是否“暂停”

我是如何做到的:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-29
    • 2014-09-14
    • 1970-01-01
    • 2023-04-08
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多