我正在了解有关共享偏好和会话管理的更多信息,因此我正在跟进此tutorial。但是我有问题在longin屏幕中我添加了user ='test'和pass ='test'并且它将我发送到主要活动但是如果我再次点击注册按钮它再次将我发送到登录屏幕但是我想成为什么发送到个人资料活动,但它假设我已经登录。
在主活动中,操作按钮将我发送到检查登录方法
case R.id.ic_register:
session.checkLogin();
return true;
具有会话管理完整代码的签入方法
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN, false);
}
完整代码
public class SessionManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "AndroidHivePref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// User name (make variable public to access from outside)
public static final String KEY_NAME = "name";
// Email address (make variable public to access from outside)
public static final String KEY_EMAIL = "email";
// Constructor
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session
* */
public void createLoginSession(String name, String email){
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_NAME, name);
// Storing email in pref
editor.putString(KEY_EMAIL, email);
// commit changes
editor.commit();
}
/**
* Check login method wil check user login status
* If false it will redirect user to login page
* Else won't do anything\
* */
public void checkLogin(){
// Check login status
if(!this.isLoggedIn()){
// user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, Register.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
else {
Intent i = new Intent(_context, UserProfile.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
/**
* Get stored session data
* */
public HashMap<String, String> getUserDetails(){
HashMap<String, String> user = new HashMap<String, String>();
// user name
user.put(KEY_NAME, pref.getString(KEY_NAME, null));
// user email id
user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
// return user
return user;
}
/**
* Clear session details
* */
public void logoutUser(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, MainActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
/**
* Quick check for login
* **/
// Get Login State
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN, false);
}
}
登录活动(如果需要)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final AlertDialogManager alert = new AlertDialogManager();
session = new SessionManager(getApplicationContext());
usernam = (EditText) findViewById(R.id.username);
passw = (EditText) findViewById(R.id.password);
email = (EditText) findViewById(R.id.email);
Toast.makeText(getApplicationContext(), "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG).show();
btnLogin = (Button) findViewById(R.id.login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Get username, password from EditText
String username = usernam.getText().toString();
String password = passw.getText().toString();
// Check if username, password is filled
if(username.trim().length() > 0 && password.trim().length() > 0){
// For testing puspose username, password is checked with sample data
// username = test
// password = test
if(username.equals("test") && password.equals("test")){
// Creating user login session
// For testing i am stroing name, email as follow
// Use user real data
session.createLoginSession("Je", "je@gmail.com");
// Staring MainActivity
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
}else{
// username / password doesn't match
alert.showAlertDialog(Register.this, "Login failed..", "Username/Password is incorrect", false);
}
}else{
// user didn't entered username or password
// Show alert asking him to enter the details
alert.showAlertDialog(Register.this, "Login failed..", "Please enter username and password", false);
}
}
});
}