适用于Android的Firebase身份验证:createUserWithEmailAndPassword工作到很晚

时间:2019-04-24 14:16:57

标签: android firebase firebase-authentication

我为createUserWithEmailAndPassword使用Android编写了正确的代码。 我已经为帐户创建的静态最终整数常量编写了成功或失败的代码(该电子邮件已被使用):

public static final int SUCCESSFUL = 100;
public static final int EMAIL_IS_ALREADY_USED = 101;

有一种创建新帐户的方法:

public static int createAnAccount(String email, String password, final Context ctx) {
  final int[] res = new int[1];
  mAuth.createUserWithEmailAndPassword(email, password)
      .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
          if (task.isSuccessful()) {
            // User has registered successfully...
            res[0] = SUCCESSFUL;
            Toast.makeText(ctx, "First has worked - Registered!!!", Toast.LENGTH_LONG).show();
          } else {
            try {
              throw task.getException();
            } catch (FirebaseAuthUserCollisionException e) {
              // That email address is already used...
              res[0] = EMAIL_IS_ALREADY_USED;
              Toast.makeText(ctx, "First has worked - This email is already used!", Toast.LENGTH_LONG).show();
            } catch (Exception e) {
              // Undefined error...
              e.printStackTrace();
            }
          }
        }
      });
  Toast.makeText(ctx, "Second has worked - Value of res[0] = " + res[0], Toast.LENGTH_LONG).show();
  return res[0];
}

我想知道用户已成功注册或电子邮件已被使用。为此,如果SUCCESSFULtask.isSuccessful(),我想返回true,如果EMAIL_IS_ALREADY_USEDtask.isSuccessful(),则返回false,但是我不能返回onComplete()中的值,因为它是anonymous inner class method。因此,我使用了res整数数组。 res的值为SUCCESSFUL,如果为isSuccessful() is true;如果EMAIL_IS_ALREADY_USEDisSuccessful() is false

如果任何用户已经注册或使用了已经注册的电子邮件,它始终可以正常工作。我必须先看到First has worked - bla bla bla消息,然后是Second has worked - Value of res[0] = bla bla消息。但是首先处理Second has worked - ...消息,然后处理First has worked - ...消息。我知道这个问题。问题在于Second has worked方法之前,return res[0]吐司消息和onComplete()正在工作。我的意思是,return res[0]中没有给出res[0]的值之前工作的onComplete。 我该如何解决这个问题?

0 个答案:

没有答案
相关问题