Google+ sign out from a different activity

前端 未结 9 1705
终归单人心
终归单人心 2020-12-05 00:17

I have started using the Google+ API for android, and I have created a sign-in application following this tutorial:

https://developers.google.com/+/mobi

9条回答
  •  一生所求
    2020-12-05 00:40

    After struggling for over a week to find out the answer. I did this, After signing in save boolean isSignedIn in sharedpreferences as true.

    private SharedPreferences.Editor editor;
    private SharedPreferences prefs;
    
    editor = getSharedPreferences(getString(R.string.userDetails), MODE_PRIVATE).edit();
    editor.putBoolean(getString(R.string.isSignedIn), true);
    editor.apply();`
    

    Now from any activity when the user clicks logout, change the boolean to false.

    In your Login Activity where googleApiClient is build. In its onStart method. Check if isSignedIn is false.

    @Override
    public void onStart() {
    super.onStart();
    if (!prefs.getBoolean(getString(R.string.isSignedIn), false)) {
        signOut();
        }     
    }
    

    Do the same in onConnected

    @Override
    public void onConnected(Bundle connectionHint) {
        if (mGoogleApiClient.isConnected()) {
            Log.i(TAG, "onConnected: " + "yes it is connected");
            if (!prefs.getBoolean(getString(R.string.isSignedIn), false)) {
                signOut();
            }
        }
    }
    

    This will logout and revokeAccess.

    public void signOut() {
        if (mGoogleApiClient != null) {
            Log.e(TAG, "signOut: " + mGoogleApiClient + mGoogleApiClient.isConnected());
            Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
            if (mGoogleApiClient.isConnected()) {
                        new ResultCallback() {
                            @Override
                            public void onResult(Status status) {
                                // ...
                                Log.i(TAG, "onResult: " + mGoogleApiClient);
                            }
                        });
             Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
                        new ResultCallback() {
                            @Override
                            public void onResult(Status status) {
                                Log.i(TAG, "onResult: Revoke Access status:" + status.getStatus());
                            }
                        });
            }
        }
    }
    

提交回复
热议问题