我为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];
}
我想知道用户已成功注册或电子邮件已被使用。为此,如果SUCCESSFUL
是task.isSuccessful()
,我想返回true
,如果EMAIL_IS_ALREADY_USED
是task.isSuccessful()
,则返回false
,但是我不能返回onComplete()
中的值,因为它是anonymous inner class method
。因此,我使用了res
整数数组。 res
的值为SUCCESSFUL
,如果为isSuccessful() is true
;如果EMAIL_IS_ALREADY_USED
为isSuccessful() 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
。
我该如何解决这个问题?