7

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

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

退出

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

但是当用户重新启动应用程序并且他已经登录(并且之前没有注销)时,是否可以检测到这种“当前登录”状态?

显然,可以在应用程序的设置(共享首选项)中保存“登录”,但是有什么方法可以使用 google api 进行检测?

4

2 回答 2

10

在这里我找到了解决方案:

  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);
                }
            });
        }
于 2016-02-04T10:19:47.907 回答
2

在这里,我找到了简单的解决方案

GoogleSignInAccount lastSignedInAccount= GoogleSignIn.getLastSignedInAccount(context);
if(lastSignedInAccount==null){
// user has already logged in, you can check user's email, name etc from lastSignedInAccount
String email = lastSignedInAccount.getEmail();
}else{
// user is not logged in with any account
}
于 2018-01-05T05:35:54.163 回答