如何使用我的Android应用程序中的菜单项从谷歌帐户注销

时间:2018-04-25 07:24:58

标签: android google-login

在我的应用程序中,我正在使用谷歌帐户登录,我的要求是我想使用菜单项(注销)从应用程序注销。

这是我的登录活动代码:

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

这里我希望使用菜单项从应用程序注销我从菜单项(注销)调用注销方法。

我搜索了这个,我找到了一些解决方案,但没有人帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

logOut / SignOut实际上有选项。 作为Docs州,您可以使用googleSignInClient上的signOut()方法退出用户

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

此处onComplete(),您可以检查它是失败还是成功,以及检查它是哪个帐户。

如果您想从他的帐户中删除您的应用(从技术上删除您对该用户帐户的应用访问权限),您可以使用revokeAccess()方法执行此操作:

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

强烈推荐revokeAccess()我建议您为此方法的用户提供一个选项