【问题标题】:Is it still necessary to call GoogleApiClient connect() and disconnect()?是否还需要调用 GoogleApiClient connect() 和 disconnect()?
【发布时间】:2019-12-26 08:54:38
【问题描述】:

我知道 GoogleApiClient 已弃用,应该使用的新类是 GoogleSignInClient,正如 Paresh Mayani 在此 answer 中所解释的那样。但是,当我在后面使用 GoogleApiClient 时,我总是在 MainActivity 中调用:

googleApiClient.connect();

在 onStart() 中:

googleApiClient.disconnect();

在 onStop() 中。

这还需要吗?

【问题讨论】:

    标签: android firebase firebase-authentication google-authentication


    【解决方案1】:

    GoogleApiClientGoogleSignInClient 是不同的。由于您使用的是GoogleSignInClient,因此无需连接/断开GoogleApiClient

    也供参考:

    GoogleApiClient 与多种静态方法一起使用。一些 这些方法需要连接 GoogleApiClient,有些会 在连接 GoogleApiClient 之前排队呼叫;检查具体 API 文档以确定您是否需要连接。

    这是 GoogleAPIClient 内部工作原理的演示

    参考链接:https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient

    按照 Google APIs for Android 文档的建议,请迁移到 GoogleApi。以下是迁移链接:

    迁移链接: https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApi

    我希望这会有所帮助。

    【讨论】:

    • 我已经阅读了文档,但我仍然不明白。那么它是否仍然需要?谢谢
    • 这完全取决于您使用的GoogleApiClient 的功能,如果该特定方法需要连接作为先决条件,那么您需要GoogleApiClient 处于connected 状态.我建议您将GoogleApiClient 保持在连接状态,这样您就可以使用所有需要GoogleApiClient 连接或未连接的方法。
    • 为您的回答和评论投了赞成票,但我还是不明白。过去,我使用的是 GoogleApiClient,现在我的项目中没有任何与此相关的内容,因为我使用的是 GoogleSignInClient 类。仅使用此类,是否仍需要保持某些连接?这种做法是否需要在应用启动/停止时连接/断开连接?
    • 感谢您的支持。是的,连接和断开GoogleApiClient 对于 Activity/Fragment 的生命周期很重要。因为GoogleApiClient 是将您的应用程序连接到与Google Play Services 捆绑的服务的包装器,Google Sign-in 就是其中之一。为了您的方便,我还编辑了答案。
    • 好的,非常感谢您回答我的问题,我现在很好。请使用文档中的一些相关信息更新您的答案,以便我接受。
    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 2014-03-02
    • 2011-04-01
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    相关资源
    最近更新 更多