【问题标题】:Catch GoogleApiClient.disconnect() callback捕捉 GoogleApiClient.disconnect() 回调
【发布时间】:2016-05-14 00:24:34
【问题描述】:

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

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

}

据我了解,这个新 API 会阻止在调用 mGoogleApiClient.disconnect 时调用 onConnectionFailed。 我可以在disconnect() 函数中使用回调吗?这是正常的行为吗?

即:

 protected void disconnect() {
            if (mGoogleApiClient != null) {
                    mGoogleApiClient.disconnect();
                    myCallbackhere();//this callback
            }
        }

【问题讨论】:

    标签: android google-api google-drive-api google-signin


    【解决方案1】:

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

    @Override
    public void onConnectionSuspended(int cause) {
    }
    

    onConnectionSuspended 在您的应用断开连接时被调用 Google Play 服务包(不一定是互联网)。

    检查这个stackoverflow link

    @Override
    public void onConnectionFailed(ConnectionResult result) {
    }
    

    当方法失败时调用 onConnectionFailed,例如 DriveApi.newDriveContents 等。

    支持检查错误或显示errorDialog,或解决后重试

    检查这个quickstart link

    很遗憾,断开连接没有回调

    但 googleDrive 退出方法不在网络中。这样您就不用担心在不知情的情况下退出

    在 disconnect() 之后调用你的方法

    尝试disconnect(),在没有wifi、数据的情况下连接

    这个googleTopic 可能会帮助你

    【讨论】:

      猜你喜欢
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 2016-12-12
      • 2012-09-30
      • 2021-02-02
      • 2021-07-10
      • 1970-01-01
      • 2012-04-27
      相关资源
      最近更新 更多