【问题标题】:GoogleApiClient onConnectionSuspended , should i call mGoogleApiClient.connect() again?GoogleApiClient onConnectionSuspended ,我应该再次调用 mGoogleApiClient.connect() 吗?
【发布时间】:2014-11-21 06:19:23
【问题描述】:

我在服务中使用 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 ? ? 

}

在上面的链接(ConnectionCallback 文档)中它说:

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

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

【问题讨论】:

    标签: android google-play-services google-api-client


    【解决方案1】:

    GoogleApiClient 将自动尝试重新连接。您无需再次拨打connect()

    【讨论】:

    • 您好 Hounshell,感谢您的回答,但您如何确定呢?有没有医生这么说的?
    • 我是 GoogleApiClient 的原作者。我将尝试为下一个版本添加一个文档说明,以澄清这一点。
    • 嗨@Hounshell,然后请从onConnectionSuspended 中删除connect() 调用quickstart apps,谢谢。
    • 有趣的是,我今天刚刚在github.com/googlesamples/android-play-location/pull/3提交了一个Pull Request
    • 虽然GoogleApiClient 尝试重新连接,但对我来说它从未成功。我必须使用Handler 并延迟手动调用以通过1000ms 重新连接才能看到onConnected() 成功记录连接。
    【解决方案2】:

    The onConnected() doc 说:

    调用connect()后,连接请求成功完成后,会异步调用该方法。

    这意味着您必须调用 connect() 否则不会调用 onConnected()

    【讨论】:

    • 创建GoogleApiClient后确实需要调用connect。但是根据@hounshell 的回答,我们不必再次调用connect,因为它会自动调用。
    猜你喜欢
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    相关资源
    最近更新 更多