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

问题描述:

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

I am integrating Facebook and Google authentication in my android application. While launching the application, I want to check if a user is logged on to the app with Facebook or Google authentication. I got success with Facebook using the below code:

if (Profile.getCurrentProfile() != null && AccessToken.getCurrentAccessToken() != null){
        Intent i = new Intent(Splash.this, SecondActivity.class);
        startActivity(i);
        finish();
}

但是在Google上没有成功。另外,我搜索了许多答案,但大多数答案都使用Firebase进行Google身份验证。

But having no success with Google. Also, I searched for many answers but most of them were using Firebase for Google authentication.

我将如何使用Google身份验证而非Firebase来实现这一目标。

How would I achieve this using Google Authentication and not Firebase.

我们将不胜感激。
预先感谢!

Help would be appreciated. Thanks in advance!

我们可以使用 GoogleSignInApi.silentSignIn()方法,以检查登录凭据是否有效。
它返回一个 OptionalPendingResult 对象,该对象用于检查凭据是否有效。如果凭据有效,则 OptionalPendingResult isDone()方法将返回true。
然后,可以使用get方法立即获得结果(如果可用)。

We can use GoogleSignInApi.silentSignIn() method to check if the login credential is valid or not. It returns an OptionalPendingResult object which is used to check whether the credential is valid or not. If the credential is valid OptionalPendingResult's isDone() method will return true. The get method can then be used to obtain the result immediately (If it is available).

Android文档中的 OptionalPendingResult
https://developers.google.com/android/reference/com/google/android/gms/common/api/OptionalPendingResult

Android Documentation for OptionalPendingResult: https://developers.google.com/android/reference/com/google/android/gms/common/api/OptionalPendingResult

GoogleSignInApi
https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInApi

这是用于检查凭据是否有效的代码。

Here's the code for checking if the credentials are valid or not.

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