0

In My Application i am working with google account login here my requirement is I want to logout from the app using menu item (logout).

Here is my login Activity code:

       btnSignIn.setOnClickListener(this);
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .build();

            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .enableAutoManage(this, this)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .build();    
            // Customizing G+ button
            btnSignIn.setSize(SignInButton.SIZE_STANDARD);
            btnSignIn.setScopes(gso.getScopeArray());    
        }

        private void signIn() {
            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            startActivityForResult(signInIntent, RC_SIGN_IN);
        }

        public void signOut() {
            Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                    new ResultCallback<Status>() {
                        @Override
                        public void onResult(Status status) {
                            updateUI(false);
                        }
                    });
        }

        private void handleSignInResult(GoogleSignInResult result) {
            Log.d(TAG, "handleSignInResult:" + result.isSuccess());
            if (result.isSuccess()) {
                // Signed in successfully, show authenticated UI.
                GoogleSignInAccount acct = result.getSignInAccount();

                Log.e(TAG, "display name: " + acct.getDisplayName());

                personName = acct.getDisplayName();

                 email = acct.getEmail();

                Log.e(TAG, "Name: " + personName + ", email: " + email);
                try {
                    personPhotoUrl = acct.getPhotoUrl().toString();
                    Glide.with(getApplicationContext()).load(personPhotoUrl)
                            .thumbnail(0.5f)
                            .crossFade()
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(imgProfilePic);

                } catch (Exception e) {
                    imgProfilePic.setImageResource(R.mipmap.user1);
                }

                updateUI(true);
            } else {
                // Signed out, show unauthenticated UI.
                updateUI(false);
            }
        }   
        @Override
        public void onClick(View view) {    
            switch (view.getId()) {
                case R.id.btn_sign_in:
                    signIn();
                    break;
            }
        }    
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);    
            // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
            if (requestCode == RC_SIGN_IN) {
                GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                handleSignInResult(result);
            }
        }

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

            OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
            if (opr.isDone()) {     
                Log.d(TAG, "Got cached sign-in");
                GoogleSignInResult result = opr.get();
                handleSignInResult(result);
            } else {
                the sign-in has expired,
                silently.  Cross-device
                showProgressDialog();
                opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
                    @Override
                    public void onResult(GoogleSignInResult googleSignInResult) {
                        hideProgressDialog();
                        handleSignInResult(googleSignInResult);
                    }
                });
            }
        }    
        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {    
            Log.d(TAG, "onConnectionFailed:" + connectionResult);
} 
        private void showProgressDialog() {
            if (mProgressDialog == null) {
                mProgressDialog = new ProgressDialog(this);
                mProgressDialog.setIndeterminate(true);
            }    
            mProgressDialog.show();
        }   
        private void hideProgressDialog() {
            if (mProgressDialog != null && mProgressDialog.isShowing()) {
                mProgressDialog.hide();
            }
        }    
        private void updateUI(boolean isSignedIn) {  
            if (isSignedIn) {    
                Intent intent = new Intent(LoginActivity.this, OtpVerfication.class);
                intent.putExtra("username",personName);
                intent.putExtra("email",email);
                intent.putExtra("picture",personPhotoUrl);
                startActivity(intent);
                overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left);
                finish();
        } 

here i want logout from the app using menu item for this i am calling signout method from the menu item(logout).

I searched for this and i found some solutions, but there no use anyone help me to solve this problem.

chanduuu
  • 19
  • 6
  • 2
    Possible duplicate of [Logout for GoogleApiClient in Android application](https://stackoverflow.com/questions/35039247/logout-for-googleapiclient-in-android-application) – Somesh Kumar Apr 25 '18 at 07:41

1 Answers1

0

There are actually options for logOut/SignOut. AS the Docs states you can sign out users using signOut() method on googleSignInClient

mGoogleSignInClient.signOut()
        .addOnCompleteListener(this, new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                // ...
            }
        });

here in onComplete(), you can check if it was a failure or success as well as checking which account it was.

even further if you like to delete your app from his account (technically removing your apps access to the user's account) you can do so using revokeAccess() method:

mGoogleSignInClient.revokeAccess()
        .addOnCompleteListener(this, new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                // ...
            }
        });

As revokeAccess() is highly recommended I suggest you provide an option for the user for this method

seyed Jafari
  • 1,235
  • 10
  • 20