如何在多项活动中正确使用 Google Plus 登录? [英] How to correctly use Google Plus Sign In with multiple activities?

查看:26
本文介绍了如何在多项活动中正确使用 Google Plus 登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将 Google+ api 客户端生命周期与多活动应用程序的流程联系起来的好/推荐方法是什么?使活动依赖于 onConnected api 客户端方法来触发其功能,将其用作一次性激活"的东西,或者完全是其他的东西?

我目前正在努力了解如何在我的 Android 应用中正确使用 Google+ 登录,该应用有多个活动.

I am currently struggling to understand how to correctly use the Google+ sign in in my Android app, which has more than one activity.

我们的想法是,在第一阶段,使用 G+ 登录只是为了对用户进行身份验证并能够获取她的电子邮件、发送通知等.最终,我计划推出其他 Google 功能,例如地图或其他 Google Play 服务,因此我认为已经实施它很有用.

The idea is, in a first phase, to use the G+ sign in just to authenticate the user and be able to get her email, to send notifications and stuff like that. Eventually I plan to roll out other Google functionality like maybe Maps or other Google Play services, so I think it's useful to implement it already.

但是,我的应用未按预期运行,我已将问题缩小到以下事实:当存在多个活动时,我尚未理解 G+ 登录应用周期.

However, my app is not behaving as expected, and I have narrowed down the issue to the fact that I have not yet understood the G+ sign in app cycle when more than one activity is present.

实现此身份验证方法的正确或推荐方法是什么?是否有某种模式可以引导我朝着正确的方向前进?

What is the correct or recommended way to implement this auth method? is there maybe a pattern of sorts that could guide me in the right direction?

例如,我发现 一个非常简单的api客户端生命周期图,但这与应用流程有什么关系?

For example, I have found a very simple diagram of the life cycle of the api client, but how does this relate to the app flow?

最初我有一个登录活动,我在其中放置了登录按钮.按照 Google 指南,我可以登录,并且当 onConnected方法被调用,我启动家庭活动(有点像应用程序的仪表板或主屏幕).

Initially I have a Login Activity, where I put the sign in button. Following Google's guide I am able to sign in, and when the onConnected method is called, I start the Home Activity (kinda like the dashboard or main screen of the app).

这有点管用.例如,处理每个活动的 onStart 和 onStop 的好方法是什么?我应该每次为每个活动重新连接并重新验证 api 客户端吗?因此,使用 BaseActivity 来实现所有这些可能是个好主意.

This works somewhat. For example, what would be a good way of handling the onStart and onStop for each activity? should I re-connect and re-authenticate the api client every time for every activity? So maybe its a good idea to have a BaseActivity to implement all this.

另一个问题是,我应该使用相同的 api 客户端对象并以某种方式传递它,还是将其存储在 Base Activity 类中?或者我应该每次都创建和初始化一个新的 api 客户端对象?

Another issue is, should I use the same api client object and pass it around somehow, or maybe store it in the Base Activity class? or should I be creating and initializing a new api client object every time?

如何仅使用登录活动通过 G+ 进行身份验证,然后获取电子邮件并将其存储在本地数据库中,并将用户标记为已验证"或活动"或其他内容.这将防止我每次关闭应用程序或暂停连接时都必须重新进行身份验证,甚至可以节省一些电池电量.

How about just using the Login Activity to authenticate with G+ and then just get the email and store it in a local database, and flag the user as "authenticated" or "active" or something. That would prevent me from having to re-authenticate every time the app is closed or connection is suspended, even allowing for some battery savings.

该应用并未真正使用 G+ 发布或任何其他类似功能.理想情况下,它应该可以很好地离线工作,并且只需要连接以进行初始身份验证或其他一次性事项.

The app is not really using G+ posting or any other functionality like that. Ideally it should work well offline, and only need connection for stuff like initial authentication or other one-time only things.

非常感谢任何正确方向的建议或指示.

Any suggestions or pointers in the right direction are very much appreciated.

我已经阅读了我能找到的所有使用 Google+ 的指南和教程,并且每个指南和教程都从单一活动的角度解决了这个问题.我认为这是一个足够普遍的问题,它会受益于模式或至少是一般准则.

I have read every guide and tutorial I could find, that uses Google+, and every one of them addresses this from a single activity perspective. I would think this is a common enough problem that it would benefit from a pattern or at least a general guideline.

推荐答案

为每个活动重新连接绝对没问题.大体上,我看到人们通过 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.

这篇关于如何在多项活动中正确使用 Google Plus 登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆