【问题标题】:Android auto-logout when app goes to background应用程序进入后台时Android自动注销
【发布时间】:2012-01-23 07:14:27
【问题描述】:

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

...但是...与往常一样,有一个客户想要某些行为...

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

那么,如果我不能钩住 HOME 键,还有哪些其他选择?显然,仅仅钩住onPause() 并没有帮助,因为那是Activity 特定的。

我们想出的“最好的”方法是在我们的 Application 类中保留一个 Activity 引用数组。在每个 Activity 的 onResume() 中,我们将其添加到此数组中。在onPause() 我们将其删除。同样在onPause() 中,我们通过这个数组进行枚举,以确定是否有任何已注册的活动在前台。如果没有找到前台活动,用户将被注销。

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

【问题讨论】:

    标签: android


    【解决方案1】:

    //使用服务

    //在那个

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

    【讨论】:

    • 我喜欢这里的想法,但是还有其他方法可以让活动进入后台 - 例如另一项活动脱颖而出。
    【解决方案2】:

    我们根据@peceps 的解决方案找到了一些解决方案:Run code when Android app is closed/sent to background

    【讨论】:

    • 我刚刚在这个线程中添加了一条新评论,我在其中展示了一个更好的方法(在我看来),使用 Application.ActivityLifecycleCallbacks 以及 @peceps 方法。请参阅我的回答:stackoverflow.com/a/13996042/327386
    【解决方案3】:

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

    如果您需要在物理上执行注销(即在远程服务器上),请在活动暂停时设置 AlarmManager 以在未来 x 秒后注销。如果另一个活动在触发之前开始,您可以取消此警报。

    【讨论】:

    • 在您的活动中添加时间戳的想法太棒了!
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      我以前曾解决过同样的问题,但据我所知,除了您现在使用的方式外,没有其他方法可以解决。捕获活动显示状态的最佳覆盖方法是 onStart(),onStop() 此方法捕获真正的可见性变化并计算您的活动堆栈计数以注销。

      【讨论】:

        【解决方案6】:

        这应该会有所帮助:

        这是在 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.
        

        【讨论】:

        • 他指出,Activity 方法行不通,因为他使用了多个活动。使用 onPause 还是 onUserLeaveHint 并不重要。唯一真正的区别是第二个被解雇的情况比第一个少。
        【解决方案7】:

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

        1. 创建基础Activity 并覆盖其onStop() 方法以从服务器注销
        2. 我的应用的所有Activities 都将扩展至基类之上。

        所以现在如果任何 Activity 进入 onStop 状态会发生什么,无论 Activity 如何进入后台,它都会从服务器注销

        注意:但是如果你从你的代码中通过调用完成停止任何活动,那么它也会注销,所以你必须在那种情况下锻炼

        【讨论】:

        • thx - 我了解 Android 活动的生命周期 - 问题是试图找到一种解决应用程序生命周期的方法,以及 Android 设计人员决定不让应用程序挂钩 HOME 按钮.您在笔记中提到的逻辑正是我试图通过提出问题来解决的问题。我正在寻找一种优雅的方式来做到这一点。
        • 在您的 BaseActivity 中创建一个方法,在您完成 Activity 时将布尔值设置为 true 并根据该值注销检查 BaseActivity 的 onStop 中的布尔值是没有问题的,反之亦然
        【解决方案8】:

        android:clearTaskOnLaunch 可能会有所帮助。当您进入后台时它不会将您注销,但您可以在您刚返回时强制登录屏幕成为第一个屏幕。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-08-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-11
          • 2011-07-27
          相关资源
          最近更新 更多