GoogleApiClient onConnectionSuspended,我应该再次调用mGoogleApiClient.connect()吗?

Tou*_*rki 21 android google-api-client google-play-services

我在服务中使用GoogleApiClient来请求融合位置更新.每件事都正常工作,但有时连接被挂起并调用onConnectionSuspended.

@Override
public void onCreate() {
    ...
    mGoogleApiClient = new GoogleApiClient.Builder(this) // this is a Context
    .addApi(LocationServices.API)
    .addConnectionCallbacks(this)  // this is a [GoogleApiClient.ConnectionCallbacks][1]
    .addOnConnectionFailedListener(this) //
    .build();

    mGoogleApiClient.connect();

    ...
}

@Override
public void onConnectionSuspended(int arg0) {

    // what should i do here ? should i call mGoogleApiClient.connect() again ? ? 

}
Run Code Online (Sandbox Code Playgroud)

在上面的链接(ConnectionCallback doc)中,它说:

应用程序应禁用需要该服务的UI组件,并等待对onConnected(Bundle)的调用以重新启用它们.

但是这个对onConnected的调用将如何发生呢?我应该再次拨打mGoogleApiClient.connect()吗?或者即使在连接暂停后,mGoogleApiClient仍将继续尝试连接?

Hou*_*ell 36

GoogleApiClient会自动尝试重新连接.你不需要再打电话connect().

  • 我是GoogleApiClient的原作者.我将尝试为下一个版本添加文档说明,以澄清这一点. (23认同)
  • 嗨@Hounshell,请从[quickstart apps]中的`onConnectionSuspended`中删除`connect()`调用(https://github.com/googleplus/gplus-quickstart-android/blob/master/app/src/main /java/com/google/android/gms/plus/sample/quickstart/MainActivity.java)然后,谢谢. (3认同)
  • 有趣的是,我今天刚刚在https://github.com/googlesamples/android-play-location/pull/3上提交了一份Pull请求. (2认同)
  • 虽然GoogleApiClient _attempts_重新连接,但对我而言却从未成功。我必须使用`Handler`并延迟手动调用以`1000ms`重新连接,才能看到`onConnected()`成功记录连接。 (2认同)