简体   繁体   English

如果用户已经登录,则在我的Android应用程序中使用谷歌登录会遇到麻烦

[英]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.

相关问题 如何从我自己的 android 应用程序在 wordpress 中登录(或检查用户是否已登录) - How to login(or check if a user is already logged in ) in wordpress from my own android app 在 Android Studio 中,如何检查用户是匿名登录还是使用 email 或 google 登录? - In Android Studio, how to check if user logged in anonymously or with email, or google sign in? 如何检查用户是否成功注销了Android的Google登录? - How to check if the user is successful logged out of the Google Sign In Android? 从Android应用注销Google用户 - Sign out google user from android app android检查用户是否已经登录 - android Check if user is already Logged in 如何签署我已经制作的android app / .apk文件 - How to sign my already made android app/.apk file 无法在 Android 应用程序中使用 Google 登录 - Unable to sign-in using Google in Android App 在Android应用和API之间使用Google登录 - Using Google sign in between Android app and api 如果已经登录,如何跳过 Google 登录? - How can I skip the Google sign in if already logged in? Android:根据用户是否登录,更改活动启动我的应用程序? - Android: Changing activity start my app depending on whether the user is logged in or not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM