Android 如何检查用户是否已使用Google帐户登录

Android 如何检查用户是否已使用Google帐户登录,android,google-authentication,android-googleapiclient,Android,Google Authentication,Android Googleapiclient,我正在我的android应用程序中集成Facebook和Google身份验证。 启动应用程序时,我想检查用户是否通过Facebook或Google身份验证登录到该应用程序。我使用以下代码成功使用Facebook: if (Profile.getCurrentProfile() != null && AccessToken.getCurrentAccessToken() != null){ Intent i = new Intent(Splash.this, Sec

我正在我的android应用程序中集成Facebook和Google身份验证。 启动应用程序时,我想检查用户是否通过Facebook或Google身份验证登录到该应用程序。我使用以下代码成功使用Facebook:

if (Profile.getCurrentProfile() != null && AccessToken.getCurrentAccessToken() != null){
        Intent i = new Intent(Splash.this, SecondActivity.class);
        startActivity(i);
        finish();
}
但是谷歌没有成功。此外,我搜索了很多答案,但大多数答案都是使用Firebase进行谷歌认证

我如何使用谷歌认证而不是Firebase来实现这一点

我们将不胜感激。 提前谢谢

@覆盖
  @Override
    public void onStart() {
        super.onStart();

        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);
                }
            });
        }
    }
public void onStart(){ super.onStart(); OptionalPendingreult opr=Auth.GoogleSignInApi.silentSignIn(MGoogleAppClient); if(opr.isDone()){ //如果用户的缓存凭据有效,则OptionalPendingreult将“完成” //谷歌搜索结果将立即发布。 Log.d(标记“已缓存登录”); GoogleSignInResult结果=操作获取(); handleSignInResult(结果); }否则{ //如果用户以前未在此设备上登录或登录已过期, //此异步分支将尝试以静默方式登录用户。跨设备 //此分支中将发生单点登录。 showProgressDialog(); 操作setResultCallback(新的ResultCallback(){ @凌驾 公开作废结果(GoogleSignInResult GoogleSignInResult){ hideProgressDialog(); handleSignInResult(谷歌SignInResult); } }); } }
我们可以使用
GoogleSignInApi.silentSignIn()
方法检查登录凭据是否有效。 它返回一个
OptionalPendingreult
对象,用于检查凭证是否有效。如果凭证有效
OptionalPendingResult
,则
isDone()
方法将返回true。 然后可以使用get方法立即获得结果(如果可用)

适用于
OptionalPendingreult
的Android文档:

GoogleSignInApi的Android文档:

下面是检查凭据是否有效的代码

OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(google_api_client);
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);
}
OptionalPendingResult opr=Auth.GoogleSignInApi.silentSignIn(谷歌api客户端);
if(opr.isDone()){
//如果用户的缓存凭据有效,则
//OptionalPendingreult将“完成”,并且
//谷歌搜索结果将立即可用。
Log.d(“标记”、“已缓存登录”);
GoogleSignInResult结果=操作获取();
handleSignInResult(结果);
}

我一直对这个问题感到困惑,很长一段时间都找不到合适的解决方案。结果证明,解决方案很短:

String strProvider = FirebaseAuth.getInstance().
         getAccessToken(false).getResult().getSignInProvider();

因此,
如果(strProvider.equals(“password”)
则通过电子邮件+密码进行身份验证,
如果(strProvider.equals(“google.com”)
则通过谷歌进行身份验证,
如果(strProvider.equals(“facebook.com”)
则通过facebook进行身份验证

附加 但是,使用这一行程序,您可以得到一个异常,可以通过添加
OnSuccessListener
来防止该异常,如下所示:

mAuth = FirebaseAuth.getInstance();
mAuth.getAccessToken(false).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
                @Override
                public void onSuccess(GetTokenResult getTokenResult) {
                    strProvider = getTokenResult.getSignInProvider();
                }
            });
mAuth=FirebaseAuth.getInstance();
mAuth.getAccessToken(false).addOnSuccessListener(新的OnSuccessListener(){
@凌驾
成功时公共无效(GetTokenResult GetTokenResult){
strProvider=getTokenResult.getSignInProvider();
}
});
最简单的方法:

 for (UserInfo user : auth.getCurrentUser().getProviderData()) {
        if (user.getProviderId().contains("facebook.com")) IS_FACEBOOK_LOGIN = true;
        else if (user.getProviderId().contains("google.com")) IS_GOOGLE_LOGIN = true;
    }

GoogleSignInApi的silentSignIn方法可用于检查用户缓存凭据的有效性。@SudheeshR请您详细说明一下。我们可以使用GoogleSignInApi.silentSignIn()方法检查登录凭据是否有效。它返回一个OptionalPendingreult对象,用于检查凭证是否有效。如果凭证有效,OptionalPendingreult的isDone()方法将返回true。然后,可以使用get方法立即获得结果(如果可用)。Google的Android文档SignInapi:OptionalPendingreult的Android文档: