2017-08-25 53 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在你的應用程序