在 Android 应用程序中注销 GoogleApiClient [英] Logout for GoogleApiClient in Android application

查看:18
本文介绍了在 Android 应用程序中注销 GoogleApiClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用这样的代码可以链接我的应用程序并使用帐户.

Using such code it is possible to link my app and use account.

if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Plus.API)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                    .addScope(Drive.SCOPE_APPFOLDER)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
        mGoogleApiClient.connect();

但是,一旦激活该帐户,有什么方法可以退出"或切换到新帐户?

But is where any way to 'logout' from this account once activated or switch to a new one?

推荐答案

clearDefaultAccount() 不起作用.要退出并清除所选帐户,请使用 Auth.GoogleSignInApi.signOut() 像这样:

clearDefaultAccount() doesn't work. For sign out and clear selected account use Auth.GoogleSignInApi.signOut() like this:

private GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Build a GoogleApiClient with access to the Google Sign-In API and the
    // options specified by gso.
    mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext()) //Use app context to prevent leaks using activity
            //.enableAutoManage(this /* FragmentActivity */, connectionFailedListener)
            .addApi(Auth.GOOGLE_SIGN_IN_API)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

private void signOut() {
    if (mGoogleApiClient.isConnected()) {
        Auth.GoogleSignInApi.signOut(mGoogleApiClient);
        mGoogleApiClient.disconnect();
        mGoogleApiClient.connect();
    }
}

这篇关于在 Android 应用程序中注销 GoogleApiClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆