抓住GoogleApiClient.disconnect()回调

时间:2016-02-04 11:47:39

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

我想抓住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无法在调用onConnectionFailed时调用mGoogleApiClient.disconnect。 我可以在disconnect()函数中使用回调吗?这是正常的行为吗?

那是:

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

1 个答案:

答案 0 :(得分:0)

这两种方法都不是断开连接的回调方法。

@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可能会对您有所帮助