更新时间: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:
所有这些都有效,而且我已经看到它们都用于现实世界的应用程序中.要记住的主要事情是将 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.