简体   繁体   English

"如何使用 Google 登录从不同的活动中退出用户?"

[英]How to sign out a user from a different activity with Google Sign-in?

In my login activity, I am signing in the user using Google Sign-in.在我的登录活动中,我正在使用 Google Sign-in 登录用户。 I would like the logout button to be in a different activity, but I'm not sure how to implement this.我希望注销按钮处于不同的活动中,但我不确定如何实现这一点。 I have looked for solutions online, but all of them use a deprecated library.我在网上寻找了解决方案,但他们都使用了一个已弃用的库。

Here is the code in my login activity:这是我的登录活动中的代码:

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

Just for a reference.仅供参考。 This is what I've done to sign out a user from an activity(HomeActivity) other than LoginActivity.这就是我从 LoginActivity 以外的活动(HomeActivity)注销用户所做的工作。 What you need to do is call the signOut() method either from onCreate() or onStart() method in LoginActivty and start LoginActivty from another Activty(HomeActivity in my case) when you want user to sign out.你需要做的是调用signOut()从两种方法onCreate()onStart()方法中LoginActivty从另一个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();
    }
    ...
    ...
}

HomeActivity家庭活动

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

Hope it helps.希望能帮助到你。

In the activity where you want to do the logout, you could do something like this:在要注销的活动中,您可以执行以下操作:

findViewById(R.id.logout_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(getApplicationContext(), GoogleSignInOptions.DEFAULT_SIGN_IN);
                signOut(googleSignInClient);
                revokeAccess(googleSignInClient);
            }
        });

The definitions of signOut() and revokeAccess() can be found here .可以在此处找到 signOut( signOut()revokeAccess()的定义。

The important thing is that, in both the activities, you pass to GoogleSignIn.getClient() the application context and not the activity context.重要的是,在这两个活动中,您将应用程序上下文而不是活动上下文传递给GoogleSignIn.getClient()

Try this尝试这个

Use below code and also clear all saved data as well使用下面的代码并清除所有保存的数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM