首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >捕获GoogleApiClient.disconnect()回调

捕获GoogleApiClient.disconnect()回调
EN

Stack Overflow用户
提问于 2016-02-04 19:47:39
回答 1查看 541关注 0票数 2

我想捕获onDisconnected回调,但是onConnectionSuspendedonConnectionFailed都没有被调用。

代码语言:javascript
复制
public class mAct extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {

//i'm using this:

protected void createClient() {
        if (gso == null) {
            gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestScopes(Drive.SCOPE_FILE)
            .requestScopes(Drive.SCOPE_APPFOLDER)// required for App Folder sample
            .requestEmail()
            .requestIdToken("MYKEY.apps.googleusercontent.com")
            .build();
        }
        if (mGoogleApiClient == null) {

            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Plus.API)
                    .addApi(Drive.API)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
    }
//this function calls `onConnected`
protected void connect() {
     if (mGoogleApiClient != null) {
         if (!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()) {
             mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
         }
     }
    }

//but this is not
protected void disconnect() {
        if (mGoogleApiClient != null) {
                mGoogleApiClient.disconnect();
        }
    }
//to signout I use this

protected void signoutWorker() {
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
            Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                    new ResultCallback<Status>() {
                        @Override
                        public void onResult(Status status) {
                            //some setup and exit
                            disconnect();
                        }
                    });

//this works
    @Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "onConnected()");
}

//this not works
@Override
    public void onConnectionSuspended(int cause) {
}
//this not works too
Override
    public void onConnectionFailed(ConnectionResult result) {
    }

}

据我所知,这个新的应用程序接口阻止了在调用mGoogleApiClient.disconnect时调用onConnectionFailed。我可以在disconnect()函数中使用回调吗?这是正常行为吗?

这就是:

代码语言:javascript
复制
 protected void disconnect() {
            if (mGoogleApiClient != null) {
                    mGoogleApiClient.disconnect();
                    myCallbackhere();//this callback
            }
        }
EN

回答 1

Stack Overflow用户

发布于 2017-10-15 20:39:27

这两个方法都不是disconnect的回调方法。

代码语言:javascript
复制
@Override
public void onConnectionSuspended(int cause) {
}

当你的应用程序与谷歌Play服务包(不一定是互联网)断开连接时,会调用

onConnectionSuspended。

检查此stackoverflow link

代码语言:javascript
复制
@Override
public void onConnectionFailed(ConnectionResult result) {
}

当方法失败时,会调用

和onConnectionFailed,如DriveApi.newDriveContents等。

它支持检查错误或显示errorDialog,或在有解决方案时重试

检查此quickstart link

不幸的是,disconnect没有回调

但是googleDrive注销方法不在网络范围内。所以你不用担心在不知情的情况下注销

只需在disconnect()之后调用您的方法

尝试断开连接(),在没有wifi、数据的情况下进行连接

googleTopic可能会对您有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35200209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档