2016-07-01 157 views
1

所以我實現了Facebook登錄,現在我已經實現了Google。它已成功登錄,但是當我重新啓動應用程序時,用戶必須重新登錄。但是,如果我使用FB登錄,則它應該如此。 這裏是我的AuthListenerAndroid應用Facebook登錄記住用戶,但Google登錄不會

mAuthListener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
      FirebaseUser user = firebaseAuth.getCurrentUser(); 
      if (user != null) { 
       Log.d("AD", "User is logged in."); 
       SignUp.this.startActivity(startMainActivity); 
      } else { 
       Log.d("AD", "User is not logged in."); 
      } 
     } 
    }; 

和其他一切做與谷歌登錄:

mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .enableAutoManage(this /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() { 
       @Override 
       public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
        Toast.makeText(SignUp.this, "There has been an error connecting to Google: " + connectionResult.getErrorMessage(), Toast.LENGTH_LONG).show(); 
       } 
      } /* OnConnectionFailedListener */) 
      .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
      .build(); 
    SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button); 
    signInButton.setOnClickListener(this); 

private void signIn() { 
      Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
      startActivityForResult(signInIntent, RC_SIGN_IN); 
     } 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    mCallbackManager.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == RC_SIGN_IN) { 
     GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
     handleSignInResult(result); 
    } 

private void handleSignInResult(GoogleSignInResult result) { 
    Log.d("AD", "Signed in via google successfully:" + result.isSuccess()); 
    if (result.isSuccess()) { 
     // Signed in successfully, show authenticated UI. 
     this.startActivity(startMainActivity); 
    } else { 
     // Signed out, show unauthenticated UI. 
     Toast.makeText(this, "An error has occurred", Toast.LENGTH_SHORT).show(); 
    } 
} 

這很奇怪;我總是遇到Google的麻煩,而且別無其他:{

回答

0

我自己解決了。我錯過了firebaseAuthWithGoogle方法:

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { 
    Log.d("AD", "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) { 
        Log.d("AD", "signInWithCredential:onComplete:" + task.isSuccessful()); 

        // If sign in fails, display a message to the user. If sign in succeeds 
        // the auth state listener will be notified and logic to handle the 
        // signed in user can be handled in the listener. 
        if (!task.isSuccessful()) { 
         Log.w("AD", "signInWithCredential", task.getException()); 
         Toast.makeText(SignUp.this, "Authentication failed.", 
           Toast.LENGTH_SHORT).show(); 
        } 
        // ... 
       } 
      }); 
} 

而我卻在被覆蓋的onActivityResult就可以撥打電話:

if (result.isSuccess()) { 
      // Signed in successfully, show authenticated UI. 
      GoogleSignInAccount account = result.getSignInAccount(); 
       firebaseAuthWithGoogle(account); 
     }