我想在用户第一次登录时显示alertdialog(我正在使用firebase身份验证)。有关如何做到这一点的任何想法?我尝试使用SharedPreferences,但只是在应用程序第一次在设备上运行时显示alertdialog。我希望它是特定于用户的,以便在他们第一次登录时为每个用户显示alertdialog。
答案 0 :(得分:1)
一种方法是在登录活动上调用finish()之前显示对话框。另一种方法是检查FirebaseAuth.getInstance()。getCurrentUser()是否不返回null,然后从您刚刚调用它的活动中显示该对话框。在此之后,您将在与用户ID关联的首选项中显示"警告显示" -flag,并在显示警报之前检查其是否存在。
我很抱歉没有更具体。如果您想要更准确的答案,请发布您的代码。
答案 1 :(得分:1)
在onActivityResult
中,您可以显示progressDialog
这样的内容。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
progress = new ProgressDialog(this);
progress.setMessage("Connecting Please Wait!..");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(false);
progress.show();
firebaseAuthWithGoogle(account);
} else {
// Google Sign In failed, update UI appropriately
// ...
}
}
}
并且firebaseAuthWithGoogle(GoogleSignInAccount account)
函数是
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
progress.cancel();
// Sign in success, update UI with the signed-in user's information
//and here you can use your sharedPreference for that Account.
Log.d(TAG, "signInWithCredential:success");
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
以下代码可以检查用户是否已登录
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
startActivity(new Intent(MainActivity.this,HomeActivity.class));
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
我希望它有所帮助。
如您所见FirebaseAuth
往往帮助您在没有sharedPreference
的情况下登录,但如果您想使用它,您可以确保在应用程序中单击SignOut按钮时销毁sharedPreference