您好我正在尝试为我的应用构建一个登录系统。我试图确保在我进入下一个活动之前登录,但是如果我进入while循环,firebase似乎停止尝试登录。喜欢认真的wtf?我正在android studio btw中构建它。
以下是一些代码:
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循环时,它会让我很好地登录,但是当我在循环中包含登录时,它会永远停留在那里。那是为什么?
答案 0 :(得分:1)
使用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");
}
}
您希望在用户登录时处理数据存储并转换到其他活动。通过将该代码放入此所谓的身份验证状态侦听器,您可以在没有等待循环的情况下完成相同操作:
//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");
}
}