我已经创建了AsyncTask
来处理Sing in。问题是Firebase方法signInWithEmailAndPassword
在doInBackground
返回值后完成。我不是我的onPostExecute(Boolean task)
来回复登录任务是否成功,但是当我查看日志消息时,我可以看到onPostExecute()
在onComplete()
之前完成。
如何在onPostExecute()
和onComplete()
完成后doInBackground()
制作。{/ p>
日志消息:
01-07 12:25:43.775 4046-4241/mk.capitalria.jusufi.riaposdeveloper V/SignInActivity: doInBackground() return type is false
01-07 12:25:43.785 4046-4046/mk.capitalria.jusufi.riaposdeveloper V/SignInActivity: onPostExecute() task is false
01-07 12:25:44.965 4046-4046/mk.capitalria.jusufi.riaposdeveloper V/SignInActivity: onComplete task is successful: true
01-07 12:25:44.965 4046-4046/mk.capitalria.jusufi.riaposdeveloper V/SignInActivity: onComplete task is complete: true
AsyncTask类:
public class LoginTask extends AsyncTask<String, Void, Boolean> {
boolean isSuccessful;
boolean isComplete;
FirebaseAuth firebaseAuth;
@Override
protected void onPreExecute() {
super.onPreExecute();
firebaseAuth = FirebaseAuth.getInstance();
signInLayout.setVisibility(View.GONE);
sinInTask.setVisibility(View.VISIBLE);
}
@Override
protected Boolean doInBackground(String... strings) {
final String email = strings[0];
final String password = strings[1];
firebaseAuth.signInWithEmailAndPassword(email, password).
addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
isSuccessful = task.isSuccessful();
isComplete = task.isComplete();
Log.v(TAG, "onComplete task is successful: " + isSuccessful);
Log.v(TAG, "onComplete task is complete: " + isComplete);
}
});
Log.v(TAG, "doInBackground() return type is " + (isSuccessful && isComplete));
return isComplete && isSuccessful;
}
@Override
protected void onPostExecute(Boolean task) {
super.onPostExecute(task);
Log.v(TAG, "onPostExcecute task is: " + task);
if (!task) {
sinInTask.setVisibility(View.GONE);
signInLayout.setVisibility(View.VISIBLE);
Toast.makeText(SignInActivity.this, "Authentication failed.",
Toast.LENGTH_LONG).show();
} else {
Intent intent = new Intent(SignInActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
}
答案 0 :(得分:2)
你不应该在这里使用AsyncTask。 Firebase异步执行signInWithEmailAndPassword。它在操作结束时调用onComplete,因此onPostExecute中的所有代码都可以移动到onComplete。
答案 1 :(得分:1)
对firebaseAuth.signInWithEmailAndPassword()
的调用已经异步,无需将其包含在AsyncTask
中。不是在onPostExecute
中执行您的操作,而是使用onComplete
方法执行操作。
答案 2 :(得分:0)
正如所说,你真的不需要在这里使用AsyncTask,因为firebaseAuth.signInWithEmailAndPassword()
是非阻塞方法。这意味着您的代码不是一步一步地执行,而是在两个不同的Thread
中执行。这就是您首先获得onPostExecute
的原因以及来自Firebase
的响应后,它会将onComplete
打印到日志。
如果你真的想使用AsyncTask
,你必须等待signInWithEmailAndPassword
返回一些值,并且可以通过以下方式完成。
public class LoginTask extends AsyncTask<String, Void, Boolean> {
boolean isSuccessful;
boolean isComplete;
FirebaseAuth firebaseAuth;
final ReentrantLock loginLock = new ReentrantLock();
...
@Override
protected Boolean doInBackground(String... strings) {
...
firebaseAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
isSuccessful = task.isSuccessful();
isComplete = task.isComplete();
Log.v(TAG, "onComplete task is successful: " + isSuccessful);
Log.v(TAG, "onComplete task is complete: " + isComplete);
loginLock.unlock()
}
})
.addOnFailureListener(new OnFailureListener {
@Override
public void onFailure(Exception e) {
loginLock.unlock();
}
});
loginLock.lock();
Log.v(TAG, "doInBackground() return type is " + (isSuccessful && isComplete));
return isComplete && isSuccessful;
}
...
}
答案 3 :(得分:0)
@Override
protected void onPostExecute(Boolean task) {
super.onPostExecute(task);
Log.v(TAG, "onPostExcecute task is: " + task);
firebaseAuth.signInWithEmailAndPassword(email, password).
addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
isSuccessful = task.isSuccessful();
isComplete = task.isComplete();
Log.v(TAG, "onComplete task is successful: " + isSuccessful);
Log.v(TAG, "onComplete task is complete: " + isComplete);
task=isComplete && isSuccessful;
if (!task) {
sinInTask.setVisibility(View.GONE);
signInLayout.setVisibility(View.VISIBLE);
Toast.makeText(SignInActivity.this, "Authentication failed.",
Toast.LENGTH_LONG).show();
} else {
Intent intent = new Intent(SignInActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
});
}
将firebaseAuth.signInWithEmailAndPassword
添加到onPostExecute
,因为在doInBackground方法控件中不会等待onComplete
被调用。因此,如果您希望在某些条件基础上执行onComplete响应后执行onPost
代码,请更改上面的onPost
。
答案 4 :(得分:0)
这里不需要AsyncTask。 Firebase的createUserWithEmailAndPassword已经异步工作。您可以在OnCompleteListener的onComplete函数中执行所有操作。以下是可用于onComplete的代码,并删除了AsyncTask。
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Intent intent = new Intent(SignInActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
sinInTask.setVisibility(View.GONE);
signInLayout.setVisibility(View.VISIBLE);
Toast.makeText(SignInActivity.this, "Authentication failed.",
Toast.LENGTH_LONG).show();
}
}