在Android应用程序中注销GoogleApiClient

Vya*_*lav 3 android google-api google-api-client

使用此类代码可以链接我的应用程序和使用帐户.

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();
Run Code Online (Sandbox Code Playgroud)

但是,一旦激活或切换到新帐户,该帐户是否有任何"退出"的方式?

Gon*_*alo 17

clearDefaultAccount()不起作用.对于退出和清除选定的帐户使用Auth.GoogleSignInApi.signOut()如下:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)


Vya*_*lav 5

我找到了这个最新的解决方案:

if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
                        mGoogleApiClient.clearDefaultAccountAndReconnect().setResultCallback(new ResultCallback<Status>() {

                            @Override
                            public void onResult(Status status) {

                                mGoogleApiClient.disconnect();
                            }
                        });

                    }  
Run Code Online (Sandbox Code Playgroud)

用法

Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
Run Code Online (Sandbox Code Playgroud)

已弃用

https://developers.google.com/android/reference/com/google/android/gms/plus/Account