-
StackOverflow 文件
-
Android 教程
-
Firebase
-
更新 Firebase 使用者的電子郵件
public class ChangeEmailActivity extends BaseAppCompatActivity implements ReAuthenticateDialogFragment.OnReauthenticateSuccessListener {
@BindView(R.id.et_change_email)
EditText mEditText;
private FirebaseUser mFirebaseUser;
@OnClick(R.id.btn_change_email)
void onChangeEmailClick() {
FormValidationUtils.clearErrors(mEditText);
if (FormValidationUtils.isBlank(mEditText)) {
FormValidationUtils.setError(null, mEditText, "Please enter email");
return;
}
if (!FormValidationUtils.isEmailValid(mEditText)) {
FormValidationUtils.setError(null, mEditText, "Please enter valid email");
return;
}
changeEmail(mEditText.getText().toString());
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mFirebaseUser = mFirebaseAuth.getCurrentUser();
}
private void changeEmail(String email) {
DialogUtils.showProgressDialog(this, "Changing Email", "Please wait...", false);
mFirebaseUser.updateEmail(email)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
DialogUtils.dismissProgressDialog();
if (task.isSuccessful()) {
showToast("Email updated successfully.");
return;
}
if (task.getException() instanceof FirebaseAuthRecentLoginRequiredException) {
FragmentManager fm = getSupportFragmentManager();
ReAuthenticateDialogFragment reAuthenticateDialogFragment = new ReAuthenticateDialogFragment();
reAuthenticateDialogFragment.show(fm, reAuthenticateDialogFragment.getClass().getSimpleName());
}
}
});
}
@Override
protected int getLayoutResourceId() {
return R.layout.activity_change_email;
}
@Override
public void onReauthenticateSuccess() {
changeEmail(mEditText.getText().toString());
}
}