如何正确使用谷歌加号有多个活动? [英] How to correctly use Google Plus Sign In with multiple activities?

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

问题描述

TL;博士会是什么占用了Google+ API客户生命周期的一个多活动的应用程序流量的好/推荐的方法?使活动依赖于onConnected API客户端的方式来触发它的功能,把它作为一个一次性的激活的东西,或者是别的东西完全?

我目前正在努力理解如何正确使用Google+登录在我的Andr​​oid应用程序,其中有一个以上的活动。

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 +号只是为了验证用户,能够得到她的电子邮件,发送通知之类的东西。最后,我打算推出谷歌的其他功能,例如,也许地图或其他谷歌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 +符号的应用程序循环,当一个以上的活动是present。

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?

起初我有一个登录活动,在这里我把登录按钮。继谷歌的指南我能够登录,而当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客户端对象,并以某种方式通过它周围,或者将其存储在基本活动类的?或者我应该创建每次初始化一个新的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 +进行身份验证,然后就收到电子邮件,并将其存储在本地数据库中,并标记用户为认证或主动什么的。这将$ P $不必每个应用程序被关闭或连接已暂停时重新进行身份验证,甚至允许一些电池节省pvent我。

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.

在朝着正确的方向任何建议或指针是非常AP preciated。

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. 实施主要是在一个baseactivity,并有其他扩展的。这是连接/断开连接的每个活动,但与code只在一个地方。
  2. 实施连接/断开连接的片段,并包括活动在需要身份验证。这是有益的,如果你已经有了一个baseactivity不能扩展(如一些游戏的情况下)。
  3. 实施连接/断开连接的服务。这可以在开火时需要broadcastintent或者类似的迹象。

所有这些工作,我看到他们都在现实世界中的应用程序使用。最主要的是要记住的是,从比较少见的分离99%的逻辑(应用要么签署或签我们,和你被告知说)登录这个present时刻的用例。因此,例如,你可能有onConnected /失败的OnConnection烧了不少,但大多是你忽视或只是翻转了一下,以应用程序的状态。只有在一个登录按钮的屏幕你需要连接的结果resoltion和onActivityResult的东西。想想谷歌播放服务的连接作为主要讲述如何在用户的状态,而不是签署他们的,你应该罚款。

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 (use is either signed in or signed our, 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 resoltion 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.

这篇关于如何正确使用谷歌加号有多个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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