gpt4 book ai didi

android - CreateUserWithEmailandPassword 在进入 while 循环时停止?

转载 作者:行者123 更新时间:2023-11-30 01:11:52 25 4
gpt4 key购买 nike

您好,我正在尝试为我的应用构建一个登录系统。我试图确保在进入下一个 Activity 之前我已登录,但如果我进入 while 循环,firebase 似乎会停止尝试登录。像认真的 wtf 吗?顺便说一句,我正在 android studio 中构建它。

这是一些代码:

void handleLogin() {
//Check our form
if (!validateForm()) {
Toast.makeText(getApplication(), "All Fields Required",
Toast.LENGTH_SHORT).show();
return;
}
//Sign in the user
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult> () {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithEmail", task.getException());
Toast.makeText(getApplication(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}

/*************************************************
* ON CREATE - The Activity runs from here!
*************************************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAuth = FirebaseAuth.getInstance();
setContentView(R.layout.activity_create_profile);

//Checking authState from Firebase
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth mAuth) {
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
isLogged = true;
System.out.println("Logged in: " + isLogged);
} else {
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};

//Pop up for when adminID is activated
adminIDField = (EditText) findViewById(R.id.editText13);
adminIDField.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
showPopUp();
else
System.out.println("Admin ID entered...");
}
});

// Create Profile Button
createProfile = (Button) findViewById(R.id.button13);
createProfile.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Calendar currentTime = Calendar.getInstance();
Calendar timeOut = (Calendar)currentTime.clone();
timeOut.add(Calendar.SECOND, 10);
emailString = (EditText) findViewById(R.id.editText7);
passwordString = (EditText) findViewById(R.id.editText8);
firstNameField = (EditText) findViewById(R.id.editText10);
lastNameField = (EditText) findViewById(R.id.editText11);
apartmentField = (EditText) findViewById(R.id.editText12);
Intent intent = new Intent("android.intent.action.MENU");

handleLogin();

while(!isLogged){ //THIS IS WHERE MY PROBLEM OCCURS
System.out.println("Authenticating...");
try{
Thread.sleep(250);
}catch (InterruptedException e){
e.printStackTrace();
}
}
//handleDataStorage();
//startActivity(intent);
}
});
}

出于某种原因,当我没有 while 循环时,它会很好地登录我,但是当我包含登录 while 循环时,它会永远保留在那里。这是为什么?

最佳答案

使用 Thread.sleep() 的自旋循环来等待某事发生是一个非常糟糕的主意。 Android(和大多数现代环境)实现了事件驱动模型,无需执行此操作。

如果您查看您的代码,它可以归结为:

 doThingA();
waitForThingA();
doSomethingThatDependsOnThingA();

在事件驱动模型中,您应该将其写成:

doThingAandThen(doSomethingThatDependsOnThingA);

如果您查看已有的代码,您会发现已遵循此模式的示例。例如,此代码响应用户的身份验证状态更改:

mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth mAuth) {
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
isLogged = true;
System.out.println("Logged in: " + isLogged);
} else {
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}

您想在用户登录时处理数据存储并转换到不同的 Activity 。通过将该代码放入这个所谓的身份验证状态监听器中,您可以在没有等待循环的情况下完成相同的操作:

//Checking authState from Firebase
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth mAuth) {
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
isLogged = true;
handleDataStorage();
//startActivity(intent);
} else {
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}

关于android - CreateUserWithEmailandPassword 在进入 while 循环时停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38342534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com