How to check whether a user has signed-in Google+?

前端 未结 3 1548
滥情空心
滥情空心 2021-01-02 03:32

I am writing an app which requires user login.

I would like to implement it by Google+ and have followed the following articles to set-up my login activity Log

相关标签:
3条回答
  • 2021-01-02 04:04

    Connect a GoogleApiClient in your MainActivity as normal, but in the onConnectionFailed instead of storing the result or resolving the error enable the sign in button. When the user clicks that, start your LoginActivity, which should also implement GoogleApiClient and related interfaces, and do all of your ConnectionResult handling from there.

    You might find it easier if you create a base activity that does the GoogleApiClient setup that both your activities implement. It's totally fine to have the GoogleApiClient connection started on each activity - its designed to be used that way.

    0 讨论(0)
  • 2021-01-02 04:16

    The silentSignIn method of GoogleSignInApi can be used to check the validity of cached credential of the user.

    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 Documentation for GoogleSignInApi:
    https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInApi

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

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

    Note - The method requires a GoogleApiClient object so please make sure that the googleapiclient object is connected before validation.

    0 讨论(0)
  • 2021-01-02 04:22

    What I have done is store a boolean value in SharedPreferences (so its persisted in case the app is finished), and use that to decide if I should present the G+ login or not.

    So basically you do what you are already doing, but in the onCreate method of you MainActivity you check if the user has logged in already. If it has just continue the activity as usual. Otherwise do the same as the tutorial

    Edit: Also, I would probably hide the revoke and signout buttons if the user has not authorized yet, as they will be useless. Only display them if it makes sense, i.e. if the user has logged in,

    0 讨论(0)
提交回复
热议问题