2016-02-04 28 views
2

我想捕获onDisconnected回调,但既不调用onConnectionSuspended也不调用onConnectionFailed捕获GoogleApiClient.disconnect()回调

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防止调用onConnectionFailedmGoogleApiClient.disconnect被调用。 我可以在disconnect()函数中使用回调吗?这是一个正常的行为吗?

即:

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

回答

0

两种方法都没有断开的回调方法。

@Override 
public void onConnectionSuspended(int cause) { 
} 

onConnectionSuspended被当你的应用程序会从 断开谷歌播放服务包(不一定是互联网)调用。

检查这个stackoverflow link

@Override 
public void onConnectionFailed(ConnectionResult result) { 
} 

和onConnectionFailed被调用时方法失败像DriveApi.newDriveContents,财产以后别的。

它支持校验错误或显示errorDialog,或重试时具有分辨率

检查这个quickstart link

不幸的是,断路不会有所回调

但Google云端硬盘登出方法是出网络。所以你不用担心退出而不自知

只是调用你的方法断开()

尝试断开()之后,连接不带WiFi,数据

googleTopic威力会帮你