且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在多项活动中正确使用 Google Plus 登录?

更新时间:2023-12-01 15:48:40

为每个活动重新连接绝对没问题.大体上,我看到人们通过 3 种方式实现此功能:

Reconnecting for each activity is absolutely fine. Broadly there are 3 ways I've seen of people implementing this:

  1. 主要在基本活动中实现,并让其他活动扩展它.这是每个活动中的连接/断开连接,但只有一个地方有代码.
  2. 在片段中实现连接/断开连接,并将其包含在需要身份验证的活动中.如果您已经拥有无法扩展的基本活动(例如某些游戏案例),这会很有帮助.
  3. 实现连接/断开连接的服务.如果需要登录,这可以触发广播意图或类似的.

所有这些都有效,而且我已经看到它们都用于现实世界的应用程序中.要记住的主要事情是将 99% 的逻辑(用户已登录或已退出,您会被告知)与相对罕见的此时登录"用例分开.因此,例如,您可能多次触发 onConnected/onConnection 失败,但大多数情况下您忽略了或只是稍微改变了应用程序的状态.只有在带有登录按钮的屏幕上,您才需要连接结果解析和 onActivityResult 内容.将 google play 服务连接视为主要是询问用户的状态,而不是让他们登录,你应该没问题.

All of these work, and I've seen them all used in real world apps. The main thing to remember is to separate the 99% logic (user is either signed in or signed out, and you are being informed of that) from the relatively rare "signing in at this present moment" use-case. So for example, you might have onConnected/onConnection failed firing a lot, but mostly you are ignoring or just flipping a bit as to the state of the application. Only on a screen with a login button do you need the connection result resolution and onActivityResult stuff. Think of the google play services connection as being mostly about asking for the state of the user, rather than signing them in, and you should be fine.