应用程序转到后台时Android自动注销

时间:2012-01-23 07:14:27

标签: android

我想检测我的应用程序何时发送到后台。关于挂钩HOME键有很多问题 - 我知道这只有通过注册为启动器应用才有可能。

......但是......总是有一个客户想要某些行为......

我们有一个具有高安全性要求的应用。无论出于何种原因,客户都希望应用程序在应用程序进入后台时退出服务器(电话,HOME键,返回上一个活动)( * * 澄清我的意思是当前面屏幕上的活动不是我应用的活动之一 * *)。

所以,如果我无法挂钩HOME键,还有其他选择吗?显然只是挂钩onPause()无济于事,因为那是Activity - 具体的。

我们提出的“最佳”是在Application类中保留一组Activity引用。在每个Activity onResume()中,我们将其添加到此数组中。在onPause()我们将其删除。同样在onPause()中,我们通过此数组枚举以查明是否有任何已注册的活动位于前台。如果未找到前台活动,则用户将被注销。

我对此作为解决方案感到不满,并希望找到更好的方法。

8 个答案:

答案 0 :(得分:6)

//使用服务

//在那

@Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

      IntentFilter filter = new IntentFilter();
     filter.addAction(Intent.ACTION_SCREEN_OFF);
     filter.addAction(Intent.ACTION_CALL);
     filter.addAction(Intent.ACTION_ANSWER);

     registerReceiver(mIntentReceiver, filter);

}

//然后在BroadcastReceiver

private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if(action.equalsIgnoreCase("android.intent.category.HOME") )
            {
//logout logic
} 
else if(action.equalsIgnoreCase("android.intent.action.SCREEN_OFF") )
            {
//logout logic
}

else if(action.equalsIgnoreCase("android.intent.action.DIAL") )
            {
//logout logic
}
else if(action.equalsIgnoreCase("android.intent.action.CALL")){
/    /logout logic
}
}

答案 1 :(得分:5)

我们最终在@peceps:Run code when Android app is closed/sent to background找到基于解决方案的内容。

答案 2 :(得分:3)

我通过在活动关闭\暂停时存储时间戳来处理它。当另一个活动开始时,它会读取时间戳,如果它的变化超过x秒,我会执行注销。

如果您需要物理执行注销(即在远程服务器上),请在活动暂停时设置AlarmManager,以便将来注销x秒。如果在其他活动开始之前启动,您可以取消此警报。

答案 3 :(得分:1)

之前我曾解决过同样的问题,但除了你现在使用的方式之外我没办法这样做。捕获活动显示状态的最佳覆盖方法是onStart(),onStop()此方法捕获真正的可见性更改并计算您的活动堆栈计数以注销。

答案 4 :(得分:0)

或者你可以使用一个单独的共享对象,并创建单个onPause()onResume()来获取/设置该共享对象上的数据。这些功能将用于所有活动“onPauseonResume

答案 5 :(得分:0)

这应该有所帮助:

这是在Activity类

中找到的方法
protected void onUserLeaveHint ()

    Since: API Level 3
    Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback.
    This callback and onUserInteraction() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.

答案 6 :(得分:0)

我不知道它是否会帮助你,但我会以这种方式尝试

  1. 创建基础Activity并覆盖其onStop()方法以从服务器
  2. 注销
  3. 我的应用的所有Activities都会超出基类。
  4. 所以现在如果任何Activity进入onStop状态会发生什么,无论Activity如何进入后台,它都将从服务器注销

    注意:但是如果你的代码通过调用finish来停止任何Activity,那么它也会注销,所以你必须在那个场景中解决

答案 7 :(得分:0)

android:clearTaskOnLaunch可能会有所帮助。当你转到后台时,它不会让你退出,但是当你刚回来时,你可以强制登录屏幕成为第一个。