-
StackOverflow 文件
-
Android 教程
-
Firebase
-
傳送 Firebase 密碼重置電子郵件
public class ForgotPasswordActivity extends AppCompatActivity {
@BindView(R.id.tIETForgotPasswordEmail)
EditText mEditEmail;
private FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
ButterKnife.bind(this);
mFirebaseAuth = FirebaseAuth.getInstance();
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
// Do whatever you want with the UserId by firebaseUser.getUid()
} else {
}
}
};
}
@Override
protected void onStart() {
super.onStart();
mFirebaseAuth.addAuthStateListener(mAuthStateListener);
}
@Override
protected void onStop() {
super.onStop();
if (mAuthStateListener != null) {
mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
}
}
@OnClick(R.id.btnForgotPasswordSubmit)
void onSubmitClick() {
if (FormValidationUtils.isBlank(mEditEmail)) {
FormValidationUtils.setError(null, mEditEmail, "Please enter email");
return;
}
if (!FormValidationUtils.isEmailValid(mEditEmail)) {
FormValidationUtils.setError(null, mEditEmail, "Please enter valid email");
return;
}
DialogUtils.showProgressDialog(this, "", "Please wait...", false);
mFirebaseAuth.sendPasswordResetEmail(mEditEmail.getText().toString())
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
DialogUtils.dismissProgressDialog();
if (task.isSuccessful()) {
Toast.makeText(ForgotPasswordActivity.this, "An email has been sent to you.", Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(ForgotPasswordActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}