首页 > 解决方案 > 如果用户已经登录,在我的 android 应用程序中使用 google 登录的问题

问题描述

如果用户已经登录,我在我的应用程序中使用谷歌登录时遇到问题。当我第一次登录时它工作得很好,因为它要求我输入密码和其他信息但是如果用户已经登录,我通常会瘦应该打开像这样的选择帐户菜单:

[截图][https://androidclarified.com/wp-content/uploads/2018/11/Screenshot_2018-11-11-20-53-15-306_com.google.android.gms_-768x1365.png]

但是在我的情况下会发生以下情况:[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;

        }
    }
}

标签: androidgoogle-signin

解决方案


您已经在 onStart() 回调方法中调用了getLastSignedInAccount方法。因此,当您使用 gmail 帐户成功登录后,每次进入此活动后,它都不会显示帐户选择对话框。所以,不用担心这个。

您可以通过以下代码注销您的用户。那么您将获得与第一次相同的登录模式。

    private void signOut() {

    mGoogleSignInClient.signOut()
            .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    // ...
                }
            });
}

推荐阅读