簡體   English   中英

"如何使用 Google 登錄從不同的活動中退出用戶?"

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

在我的登錄活動中,我正在使用 Google Sign-in 登錄用戶。 我希望注銷按鈕處於不同的活動中,但我不確定如何實現這一點。 我在網上尋找了解決方案,但他們都使用了一個已棄用的庫。

這是我的登錄活動中的代碼:

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

僅供參考。 這就是我從 LoginActivity 以外的活動(HomeActivity)注銷用戶所做的工作。 你需要做的是調用signOut()從兩種方法onCreate()onStart()方法中LoginActivty從另一個Activty(HomeActivity在我的情況)開始LoginActivty當你想用戶登出。

登錄活動

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

希望能幫助到你。

在要注銷的活動中,您可以執行以下操作:

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

可以在此處找到 signOut( signOut()revokeAccess()的定義。

重要的是,在這兩個活動中,您將應用程序上下文而不是活動上下文傳遞給GoogleSignIn.getClient()

嘗試這個

使用下面的代碼並清除所有保存的數據

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