5

enter image description here我新的Android应用程序development.I对我的Android应用小issue.Working突然袭来“Home”按钮,所以一段时间后,切换到后台再去“首页“,然后再次点击我的应用程序图标从我的第一个屏幕开始,而不是这个我保持最后一次查看活动,并应显示消息,如”您的会话超时所以请再次登录有两个EditText框的框“然后检查凭据并允许用户继续或重定向到登录屏幕。如何实现此目的。如何保持会话超时处理在Android应用程序

在此先感谢..

+0

您是否在使用服务器客户端架构? – Shrikant 2012-07-25 10:11:54

+0

不,我只是想保持登录安全多数民众赞成在所有... – Srinivasan 2012-07-25 10:18:37

回答

3

它在Android中很简单。你需要维护SharedPreferences

阅读this更好地理解概念。

这将作为

==> when ever you are trying to maintain login concept save user name in the sharedpreference. 
==> once data is edited in prefernces.xml , the data in it can be check in any Activity. 

工作。例如,为了节省用户名,密码和会话ID,您可以:

SharedPreferences pref = myContexy.getSharedPreferences("Session Data", MODE_PRIVATE); 
SharedPreferences.Editor edit = pref.edit(); 
edit.putString("User Name", username); 
edit.putString("Password", password); 
edit.putInt("Session ID", session_id); 
edit.commit(); 

,并让他们

SharedPreferences pref = myContexy.getSharedPreferences("Session Data", MODE_PRIVATE); 
username = pref.getString("User Name", ""); 
password = pref.getString("Password", ""); 
session_id = pref.getInt("Session ID", 0); 
+4

我不认为存储用户密码是安全的。这似乎不合法或不错。 – Swami 2015-08-18 21:53:33

+1

我完全同意。这并不安全。 – 2016-06-09 15:46:27

1

的活动周期的说明,请参见本图:http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

覆盖在你的活动相应的方法来跟踪,当它失去的/获得焦点。

+0

根据您的答案,应用程序将只能看到或不可见基于其活动的生命周期。但问题是如何维持会话超时,即如果会话超时,堆栈中的所有活动都将被清除,用户将被导航到登录屏幕。 – Shrikant 2012-07-25 10:11:02

2

SharedPreferences是肯定要走的路,但要进入更详细的超时:

  • 保存在onPause()当前时间戳( - >SharedPreferences
  • 然后在onResume()进行比较,以检查超时

如果没有超时只是继续,否则表明不错屏幕,只需注销,自毁设备,或任何你想要做的事情:-)

你也可以考虑实施某种类型的会话管理器类,以处理它在更高的水平: Nice example/tutorial from android hive

+1

非常明确的解释,+100为自毁/任务不可能参考 – MandisaW 2017-08-31 14:29:42

0

如果你想那么而是保持其在所有的活动中,我们可以就地处理程序做这个偏好的用户登录会话。

  1. 用户logsin
  2. 集sendMessageAtTime(消息,长)之后创建处理程序,长为毫秒值。这将调用定义的注销功能。