猿问

如何使用 Android Studio 在 firebase 中检查“用户电子邮件已经存在”

我正在尝试更改我的代码。但是,它失败了。我的输出还是一样。也就是说,如果用户放置了一个已经存在或不存在的电子邮件。验证“此电子邮件已被注册。” 还是出来了。为什么?有人我的编码有什么问题吗?


这是我的方法:-


public boolean isCheckEmail(final String email)

{

    mAuth.fetchProvidersForEmail(email).addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>()

    {

        @Override

        public void onComplete(@NonNull Task<ProviderQueryResult> task)

        {

            boolean check = !task.getResult().getProviders().isEmpty();


            if(check)

            {

                Toast.makeText(Signup.this, "This email has been registered.", Toast.LENGTH_SHORT).show();

            }


            else

            {

                Toast.makeText(Signup.this, "This email has been registered.", Toast.LENGTH_SHORT).show();

            }

        }

    });

    return true;

}

虽然这半编码,我试图回调方法 isCheckEmail:-


 private void RegisterAccount(String firstname, String lastname, String email, String password, String confirmpass)

{

    if (TextUtils.isEmpty(email))

    {

        Toast.makeText(Signup.this, "Enter your email address.", Toast.LENGTH_SHORT).show();

    }


    else if (!isValidEmail(email)) {

        Toast.makeText(Signup.this,"Please enter your valid email address.",Toast.LENGTH_SHORT).show();

    }


    else if (!isCheckEmail(email))

    {

        Toast.makeText(Signup.this, "This email has been registered.", Toast.LENGTH_SHORT).show();

    }


不负相思意
浏览 147回答 3
3回答

慕尼黑的夜晚无繁华

fetchProvidersForEmail 是一个异步调用,因此您必须在回调中使用其返回值。在主线程上返回 true 将不起作用。这是解决方案:首先用方法创建接口(成功)public interface OnEmailCheckListener(){&nbsp;void onSuccess(boolean isRegistered);}&nbsp;你的 checkEmail 方法应该是这样的:public void isCheckEmail(final String email,final OnEmailCheckListener listener){mAuth.fetchProvidersForEmail(email).addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>(){&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onComplete(@NonNull Task<ProviderQueryResult> task)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; boolean check = !task.getResult().getProviders().isEmpty();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; listener.onSuccess(check);&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }});}最后像这样调用你的 isCheckEmail :isCheckEmail("yourEmail@test.com",new OnEmailCheckListener(){&nbsp; &nbsp;@Override&nbsp; &nbsp;void onSuccess(boolean isRegistered){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;if(isRegistered){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//The email was registered before&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//The email not registered before&nbsp; &nbsp; }&nbsp; &nbsp;}});希望这对你有帮助。

手掌心

此方法可用于检查电子邮件是否存在&nbsp;void checkEmailExistsOrNot(){&nbsp; &nbsp; firebaseauth.fetchSignInMethodsForEmail(email.getText().toString()).addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG,""+task.getResult().getSignInMethods().size());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (task.getResult().getSignInMethods().size() == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // email not existed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // email existed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }).addOnFailureListener(new OnFailureListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(@NonNull Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}
随时随地看视频慕课网APP

相关分类

Java
我要回答