2016-07-01 4 views
1

Facebookのサインインを実装しました。これは正常にサインインしますが、アプリを再起動すると、再度ログインする必要があります。しかし、私がFBでログインしている場合は、そうする必要があります。 は、ここに私のAuthListenerです:AndroidアプリFacebook Sign-Inはユーザーを覚えていますが、Google Sign Inはありません

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."); 
      } 
     } 
    }; 

とGoogleのログインを行うには他のすべて:

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); 
     } 
関連する問題