我正在使用 SharedPreferences
检查用户是否登录。
如果用户未登录,他将被带到登录 Activity (或)我的信息 Activity 。
我的代码如下。
这是在登录 Activity 中。
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("logged", true); // set it to false when the user is logged out
editor.commit(); // Commit the edits!
我在主要 Activity 中有检查代码,如下所示
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
boolean loggedIn = settings.getBoolean("logged", true);
if (loggedIn != true) {
// Toast.makeText(this,"you are logged in !!",3000).show();
/* Intent i = new Intent(this,Login.class);
startActivity(i);*/
Intent intent = new
Intent(MymainScreen.this, Registered.class);
startActivity(intent);
} else {
// Toast.makeText(this,"you are not logged in !!",3000).show();
Intent intent = new
Intent(MymainScreen.this, My_info.class);
startActivity(intent);
}
请帮助我,没有这个 Activity 我无法进一步发展。
最佳答案
据我了解,您想使用 SharedPreferences
检查登录状态,默认情况下您应该认为用户已注销。因此,当 SharedPreferences
中没有任何内容时,您应该采用等于 false
而不是 true
的默认值。
SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
Intent intent = null;
if (prefs.getBoolean("logged_in", false)) { //user logged in before
intent = new Intent(this, Info.class);
} else {
intent = new Intent(this, Registration.class);
}
startActivity(intent);
希望对你有帮助!
关于android - 检查用户是否登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22605452/