2012-03-07 48 views
1

我正在构建一个本地android应用程序,我希望用户在一段不活动时间后说5分钟后自动注销(类型会话超时)。Android应用程序中的自动注销功能

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

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

+0

更多细节可以帮助人们回答这个问题。他们登录到了什么地方?是否有网络请求?一个简单的解决方案可能是简单地将时间戳存储在某个检查过期时间的地方,您可以在onPause中写入时间戳,然后在onResume中检查它,并且您的整个应用程序都可以访问它。 – Theblacknight 2012-03-07 13:00:34

+0

@Theblacknight我正在调用REST Web服务在登录时对用户进行身份验证。除此之外,在各种屏幕上还有更多的Web服务调用来检索/存储某些信息。但是,这是独立的应用程序,我没有维护任何用户会话。 – 2012-03-07 13:52:45

回答

0

好的,为了响应你发布的链接,为什么不按照这个方法,而是创建某种抽象基本活动,所有其他活动扩展。所以基本上你会为每个活动增加一个超时,但你的基本活动将处理这个,你的孩子活动不需要知道发生了什么。

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