2017-08-25 28 views
0

我想在用户第一次登录时显示alertdialog(我正在使用Firebase身份验证)。有关我如何做到这一点的任何想法?我尝试使用SharedPreferences,但只是在应用程序第一次在设备上运行时显示alertdialog。我希望它是用户特定的,以便在第一次登录时为每个用户显示alertdialog。Android - 用于验证Firebase用户的Alertdialog

+0

你尝试过这么远吗? –

回答

1

执行此操作的一种方法是在您的登录活动调用finish()之前显示对话框。另一种方法是检查FirebaseAuth.getInstance()。getCurrentUser()不返回null,然后从您现在调用它的活动中显示对话框。在此之后,您会在与用户标识关联的首选项中存储“显示的警告”标志,并在显示警报之前检查是否存在此警报。

我很抱歉没有更具体。如果您想要更精确的答案,请发布您的代码。

+0

谢谢,我会尝试你的第二个解决方案 – John

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(); 
        } 
       } 
      }); 
} 

和下面的代码是在这里你可以检查用户是否signedIn与否

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在你的应用程序