我正在開發一個將用戶數據保存在Firebase中的應用程序,但每次重新啓動應用程序時,用戶都必須再次登錄,因爲會話不會保持打開狀態,我已閱讀我可以實現SharedPreferences但我不知道如何在我的項目中做。實施SharedPreferences以保存用戶數據?
這裏是LoginActivity.java:
public class LoginActivity extends AppCompatActivity {
private static final String TAG = "LoginActivity";
// La respuesta del JSON es
private static final String username = "success";
private static final String TAG_MESSAGE = "message";
private Button btnLogin, btnLinkToSignUp;
private ProgressBar progressBar;
private FirebaseAuth auth;
private EditText loginInputEmail, loginInputPassword;
private TextInputLayout loginInputLayoutEmail, loginInputLayoutPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
auth = FirebaseAuth.getInstance();
loginInputLayoutEmail = (TextInputLayout) findViewById(R.id.login_input_layout_email);
loginInputLayoutPassword = (TextInputLayout) findViewById(R.id.login_input_layout_password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
loginInputEmail = (EditText) findViewById(R.id.login_input_email);
loginInputPassword = (EditText) findViewById(R.id.login_input_password);
btnLogin = (Button) findViewById(R.id.btn_login);
btnLinkToSignUp = (Button) findViewById(R.id.btn_link_signup);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
submitForm();
}
});
btnLinkToSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(appz.seoallinone.LoginActivity.this, SignupActivity.class);
startActivity(intent);
}
});
}
/**
* Validating form
*/
private void submitForm() {
String email = loginInputEmail.getText().toString().trim();
String password = loginInputPassword.getText().toString().trim();
if(!checkEmail()) {
return;
}
if(!checkPassword()) {
return;
}
loginInputLayoutEmail.setErrorEnabled(false);
loginInputLayoutPassword.setErrorEnabled(false);
progressBar.setVisibility(View.VISIBLE);
//authenticate user
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// If sign in fails, Log a message to the LogCat. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
// there was an error
Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
} else {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
});
}
private boolean checkEmail() {
String email = loginInputEmail.getText().toString().trim();
if (email.isEmpty() || !isEmailValid(email)) {
loginInputLayoutEmail.setErrorEnabled(true);
loginInputLayoutEmail.setError(getString(R.string.err_msg_email));
loginInputEmail.setError(getString(R.string.err_msg_required));
requestFocus(loginInputEmail);
return false;
}
loginInputLayoutEmail.setErrorEnabled(false);
return true;
}
private boolean checkPassword() {
String password = loginInputPassword.getText().toString().trim();
if (password.isEmpty() || !isPasswordValid(password)) {
loginInputLayoutPassword.setError(getString(R.string.err_msg_password));
loginInputPassword.setError(getString(R.string.err_msg_required));
requestFocus(loginInputPassword);
return false;
}
loginInputLayoutPassword.setErrorEnabled(false);
return true;
}
private static boolean isEmailValid(String email) {
return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
private static boolean isPasswordValid(String password){
return (password.length() >= 6);
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
@Override
protected void onResume() {
super.onResume();
progressBar.setVisibility(View.GONE);
}
}
我怎樣才能在這個特定的項目實現它?謝謝!
[Android文檔](https://developer.android.com/training/basics/data-storage/shared-preferences.html)是一個很好的開始。 對於更直接的事情,你可以檢查[此鏈接](http://androidexample.com/Android_SharedPreferences_Basics/index.php?view=article_discription&aid=126) – maxoumime