嘿,我正在尝试使用Google登录在Firebase上进行登录。我的问题是,当我要连接时,该应用程序不会向我显示包含所有可用电子邮件的电子邮件菜单,并且它进行连接时无法设置多个帐户。
我在下面发布我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Btn_Sign = (SignInButton) findViewById(R.id.sign);
mAuth = FirebaseAuth.getInstance();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
GoogleSignInOptions SignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
googleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this).addApi(Auth.GOOGLE_SIGN_IN_API, SignInOptions).build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
Btn_Sign.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}
});
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CODE) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
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()) {
Intent start = new Intent(MainActivity.this, Main2Activity.class);
startActivity(start);
} else {
Log.e(TAG, "signInWithCredential:failure", task.getException());
}
}
});
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
感谢您的回答。
答案 0 :(得分:1)
如果您希望始终显示对话框,则需要在登录之前断开用户连接 您可以使用此代码
GoogleSignIn.getClient(getActivity(), GoogleSignInUser.getGoogleSignInOptions(getActivity())).signOut()
.addOnCompleteListener(getActivity(), new OnCompleteListener<Void>() {
@Override
public void onComplete(@android.support.annotation.NonNull Task<Void> task) {
GoogleSignIn.getClient(getActivity(), GoogleSignInUser.getGoogleSignInOptions(getActivity())).revokeAccess()
.addOnCompleteListener(getActivity(), new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
//do what you want
}
})
.addOnCanceledListener(new OnCanceledListener() {
@Override
public void onCanceled() {
//do what you want
}
});
}
});