如何通过Google登录从其他活动中注销用户?

时间:2019-01-05 03:12:08

标签: android android-studio google-signin

在我的登录活动中,我正在使用Google登录名登录用户。我希望注销按钮处于不同的活动中,但是我不确定如何实现这一点。我一直在网上寻找解决方案,但是所有解决方案都使用不推荐使用的库。

这是我的登录活动中的代码:

public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.sign_in_button) SignInButton signInButton;

    public GoogleSignInClient mGoogleSignInClient;
    private int RC_SIGN_IN;
    private static final String TAG = "Login Activity Error";
    private static GoogleSignInOptions gso;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        ButterKnife.bind(this);
        signInButton.setSize(SignInButton.SIZE_STANDARD);

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

        mGoogleSignInClient = GoogleSignIn.getClient(getApplicationContext(), gso);

        findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent signInIntent = mGoogleSignInClient.getSignInIntent();
                startActivityForResult(signInIntent, RC_SIGN_IN);

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        updateUI(account);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        }
    }

    private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);

            updateUI(account);
        } catch (ApiException e) {
            Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
            updateUI(null);
        }
    }

    private void updateUI(GoogleSignInAccount account) {
        if (account != null) {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            intent.putExtra(MainActivity.PERSON_NAME, account.getDisplayName());
            intent.putExtra(MainActivity.PERSON_FAMILY_NAME, account.getGivenName());
            intent.putExtra(MainActivity.PERSON_GIVEN_NAME, account.getFamilyName());
            intent.putExtra(MainActivity.PERSON_EMAIL, account.getEmail());
            intent.putExtra(MainActivity.PERSON_ID, account.getId());
            intent.putExtra(MainActivity.PERSON_PHOTO, account.getPhotoUrl());
            startActivity(intent);
        }
    }
}

Google表示要使用此代码进行登出,但我不确定如何更改它以在其他活动中使用:

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

任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

仅供参考。这是我从LoginActivity以外的活动(HomeActivity)注销用户的工作。您需要做的是从LoginActivty中的signOut()onCreate()方法中调用onStart()方法,并在希望用户注销时从另一个Activty(在我的情况下为HomeActivity)启动LoginActivty。

LoginActivity

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    ...
       // I've used session to save  current user availibility in shared preference to
       //check if user is already logged in or not
    if (session.isLoggedIn()) {
        // User is already logged in. Take him to main activity
        Intent intent = new Intent(LoginActivity.this,
                HomeActivity.class);
        startActivity(intent);
        finish();
    } else {
        //This is what you need to do.
        signOut();
        revokeAccess();
    }
    ...
    ...
}

家庭活动

    private void logoutUser() {
    session.setLogin(false);
    // Launching the login activity
    Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
    startActivity(intent);
    finish();
}

希望有帮助。

答案 1 :(得分:0)

尝试一下

使用以下代码,并清除所有保存的数据

if (mGoogleApiClient.isConnected()) {
                Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                mGoogleApiClient.disconnect();
                mGoogleApiClient.connect();
            }