Ben*_*net 15 android login session-state
我在我的应用程序中使用共享首选项管理会话.如果用户已登录,则必须显示主页活动,否则必须显示登录活动.
在http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/的帮助下
如果用户未登录,我尝试创建主页并重定向到登录活动.
这是对的吗?还是有更好的解决方案.
谢谢,班纳特.
J. *_* K. 10
这是我在登录用户时所做的事情:
private static final String APP_SHARED_PREFS = "asdasd_preferences";
SharedPreferences sharedPrefs;
Editor editor;
private boolean isUserLoggedIn;
sharedPrefs = getApplicationContext().getSharedPreferences(APP_SHARED_PREFS, Context.MODE_PRIVATE);
isUserLoggedIn = sharedPrefs.getBoolean("userLoggedInState", false);
editor = sharedPrefs.edit();
editor.putBoolean("userLoggedInState", true);
editor.putInt("currentLoggedInUserId", userId);
editor.commit();
Intent signupSuccessHome = new Intent(this, Home.class);
signupSuccessHome.putExtra("reqFrom", "login");
startActivity(signupSuccessHome);
finish();
Run Code Online (Sandbox Code Playgroud)
在我的所有活动扩展的基本活动中(它包含操作栏和滑动菜单)我有以下检查:( mainactivity是我的登录/注册屏幕.如果用户没有登录我发送它们)
@Override
protected void onResume() {
sharedPrefs = getApplicationContext().getSharedPreferences(APP_SHARED_PREFS, Context.MODE_PRIVATE);
isUserLoggedIn = sharedPrefs.getBoolean("userLoggedInState", false);
if (!isUserLoggedIn) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
super.onResume();
}
@Override
protected void onRestart() {
sharedPrefs = getApplicationContext().getSharedPreferences(APP_SHARED_PREFS, Context.MODE_PRIVATE);
isUserLoggedIn = sharedPrefs.getBoolean("userLoggedInState", false);
if (!isUserLoggedIn) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
super.onRestart();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPrefs = getApplicationContext().getSharedPreferences(APP_SHARED_PREFS, Context.MODE_PRIVATE);
isUserLoggedIn = sharedPrefs.getBoolean("userLoggedInState", false);
currentlyLoggedInUser = sharedPrefs.getInt("currentLoggedInUserId", 0);
currentlyLoggedInUserString = Integer.toString(currentlyLoggedInUser);
if (!isUserLoggedIn) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
Run Code Online (Sandbox Code Playgroud)
并阻止用户返回登录屏幕:在Login.java中:
isUserLoggedIn = sharedPrefs.getBoolean("userLoggedInState", false);
if (isUserLoggedIn) {
Intent intent = new Intent(this, Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
Run Code Online (Sandbox Code Playgroud)
在Home.java中:
@Override
public void onBackPressed() {
Intent intent = new Intent(this, Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
super.onBackPressed();
}
Run Code Online (Sandbox Code Playgroud)
根据我的经验,我还使用登录页面/主页重定向的共享首选项。唯一的区别是,我的第一页是一个启动屏幕,我会显示一段时间。之后,我使用共享首选项检查登录状态,并执行必要的重定向。
不过,这里应该注意一点,某些服务器要求您在一段时间(作为登录响应的一部分从服务器发送的可配置值)过去后发送新的登录请求。所以你可能想看一下。在我的另一个应用程序中,我需要在每次启动应用程序时发送登录请求,因此我只需在第一次登录后将登录值(用户名/密码)存储在共享首选项中,然后默默地执行登录部分(无需显示登录屏幕)在启动屏幕之后。所以这一切都取决于您的要求。但在我的所有应用程序中,我仅使用共享首选项。
也许其他人可以提出更好的方法。
| 归档时间: |
|
| 查看次数: |
21076 次 |
| 最近记录: |