【问题标题】:Automatic Logout feature in Android AppAndroid 应用程序中的自动注销功能
【发布时间】:2012-03-07 12:17:43
【问题描述】:

我正在构建一个原生 android 应用程序,我希望用户在 5 分钟不活动后自动注销(会话超时)。

这是一个独立的应用程序,应用程序中有多个屏幕。我没有与服务器维护任何用户会话。

P.S:我在writing a time out event for android 中找到了可能的解决方案。但这仅适用于单个 Activity 应用程序。任何人都可以为多活动应用程序提出类似的解决方案吗?

【问题讨论】:

  • 更多细节将帮助人们回答这个问题。他们登录到什么?是否涉及网络请求?一个简单的解决方案可能是简单地将时间戳存储在您检查是否过期的地方,您可以在 onPause 中写入时间戳,然后在 onResume 中检查它,整个应用程序都可以访问它。
  • @Theblacknight 我正在调用 REST Web 服务来在登录期间对用户进行身份验证。除此之外,在各种屏幕中还有更多的 Web 服务调用来检索/存储某些信息。但是,这是一个独立的应用程序,我没有维护任何用户会话。

标签: android logout session-timeout


【解决方案1】:

好的,作为对您发布的链接的回应,为什么不遵循这种方法,而是创建某种抽象的基础活动,您的所有其他活动都可以扩展。所以本质上你是在为每个活动添加一个超时,但是你的基础活动会处理这个,你的子活动不需要知道发生了什么。

【讨论】:

    【解决方案2】:
    public class LogoutService extends Service {
        public static CountDownTimer timer;
    @Override
    public void onCreate(){
        // TODO Auto-generated method stub
        super.onCreate();
          timer = new CountDownTimer(1 *60 * 1000, 1000) {
                public void onTick(long millisUntilFinished) {
                   //Some code
                    Log.v(Constants.TAG, "Service Started");
                }
    
                public void onFinish() {
                    Log.v(Constants.TAG, "Call Logout by Service");
                    // Code for Logout
                    stopSelf();
                }
             };
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
     }
     }
    And then Add the below code in every activity.
    
    @Override
     protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    
    LogoutService.timer.start();
    }
    
    @Override
    protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    LogoutService.timer.cancel();
    }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2011-07-27
      • 2019-04-01
      • 2012-05-03
      • 2011-02-13
      相关资源
      最近更新 更多