Check whether the user is already logged in using Auth.GoogleSignInApi?

时间秒杀一切 提交于 2019-12-22 05:13:59

问题


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);

to signout

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?

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?


回答1:


Here I found the solution:

  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);
                }
            });
        }



回答2:


Here I found the simple solutions for this

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
}


来源:https://stackoverflow.com/questions/35195673/check-whether-the-user-is-already-logged-in-using-auth-googlesigninapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!