检查用户是否已经使用Auth.GoogleSignInApi登录? [英] Check whether the user is already logged in using Auth.GoogleSignInApi?

查看:253
本文介绍了检查用户是否已经使用Auth.GoogleSignInApi登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现要登录用户,我必须使用以下代码:

I fount that in order to sign in the user I have to use this code:

Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);

要注销

new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    disconnect();
                }
            });

但是当用户重新启动该应用程序并且他已经登录(并且之前没有注销)时,

But when the user relaunch the app and he is already logged in (and no sign out before) is it possible to detect this 'currently logged in' state?

很明显,可以将已登录保存在应用程序的设置(共享首选项)中,但是可以在哪里可以使用Google API进行检测?

Obviously, it is possible to save 'logged in' in the settings (shared preferences) of the app but is where any way to detect using google api?

推荐答案

这里我找到了解决方法:

  OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
        if (opr.isDone()) {
            // If the user's cached credentials are valid, the OptionalPendingResult will be "done"
            // and the GoogleSignInResult will be available instantly.
            Log.d(TAG, "Got cached sign-in");
            GoogleSignInResult result = opr.get();
            handleSignInResult(result);
        } else {
            // If the user has not previously signed in on this device or the sign-in has expired,
            // this asynchronous branch will attempt to sign in the user silently.  Cross-device
            // single sign-on will occur in this branch.
            showProgressDialog();
            opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
                @Override
                public void onResult(GoogleSignInResult googleSignInResult) {
                    hideProgressDialog();
                    handleSignInResult(googleSignInResult);
                }
            });
        }

这篇关于检查用户是否已经使用Auth.GoogleSignInApi登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆