[英]Troubles with using google sign in in my android app if user is already logged in
I have problems with using google sign-in in my application if user is already logged in. When I log in the first time it works nice since it asks me for password and other information but if user is already logged in I thin that normally it should open choose account menu like this one: 如果用户已经登录,我在我的应用程序中使用谷歌登录时遇到问题。当我第一次登录时它工作得很好,因为它要求我输入密码和其他信息但是如果用户已经登录我通常会瘦应该打开选择这样的帐户菜单:
[Screenshot][https://androidclarified.com/wp-content/uploads/2018/11/Screenshot_2018-11-11-20-53-15-306_com.google.android.gms_-768x1365.png] [截图] [https://androidclarified.com/wp-content/uploads/2018/11/Screenshot_2018-11-11-20-53-15-306_com.google.android.gms_-768x1365.png]
But what happens in my case is the following: [problem-gif][https://gph.is/g/ZYBxy14] 但在我的情况下会发生以下情况:[problem-gif] [https://gph.is/g/ZYBxy14]
public class LoginActivity extends AppCompatActivity implements
View.OnClickListener{
private static final String TAG = "LoginActivity1";
private static final int RC_SIGN_IN = 9001;
private GoogleSignInClient mGoogleSignInClient;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
SignInButton signInButton = findViewById(R.id.sign_in_button);
signInButton.setOnClickListener(this);
}
private void signIn() {
Toast.makeText(this, "button works", Toast.LENGTH_SHORT).show();
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// [START_EXCLUDE]
updateUI(null);
// [END_EXCLUDE]
}
});
}
private void updateUI(@Nullable GoogleSignInAccount account) {
if (account != null) {
Log.d(TAG, account.getDisplayName());
} else {
Log.d(TAG, "account is null");
}
}
@Override
public void onStart() {
super.onStart();
// [START on_start_sign_in]
// Check for existing Google Sign In account, if the user is already signed in
// the GoogleSignInAccount will be non-null.
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
// [END on_start_sign_in]
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
updateUI(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.d(TAG, "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
}
}
}
You already called getLastSignedInAccount method in onStart() callback method. 您已经在onStart()回调方法中调用了getLastSignedInAccount方法。 So, When you successfully log in with your gmail account,after each time you came into this activity, it does not show account choose dialog. 因此,当您使用您的Gmail帐户成功登录后,每次进入此活动后,它都不会显示帐户选择对话框。 So, No worries about this. 所以,不用担心这个。
You can log out your user by following code. 您可以通过以下代码注销您的用户。 then you will get same log in pattern what you get first time. 那么你将获得第一次获得的相同的登录模式。
private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.