坚持在活动之间登录Google Play服务 [英] Persist Google Play Services login between activities

查看:185
本文介绍了坚持在活动之间登录Google Play服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这很简单,但我一直在试图弄清楚如何让用户在 Activities 之间登录。



我有一个Main和一个Details Activity 。用户在主要活动中登录到 Google Play服务,我希望提交成就和排行榜数据Details Activity



我继承自 BaseGameActivity 活动和使用中:

  mGoogleApiClient = getApiClient() ; 

在Details中,但是当我调用 isConnected 它总是返回 false



我甚至尝试从Main中复制所有登录/ Activity 结束,但它仍然无法检测到用户已登录。



这篇文章建议不要使用 BaseGameActivity 并传递 GameHelper 使用 singleton



如何使用BaseGameActivity。 getApiClient()在多个活动中?



不确定正确的方法。

解决方案

最简单的方法是让两个活动都创建一个单独的api客户端实例。连接状态在内部被共享,所以你不需要担心如何传递客户端和处理活动未激活时可能发生的回调,并且玩家只会登录主要活动。

从BaseGameActivity扩展活动真的不再需要(对于一个有趣的解释手表: Death of BasegameActivity 。你需要做的是实现处理初始化GoogleAPIClient的两个接口:

 public class MainActivity extends Activity实现
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
}

要实现这些,请参阅示例和文档: https://developers.google.com/games/ser vices / android / init


I thought this would be straightforward, but I've been going in circles trying to figure out how to keep a user logged in between Activities.

I have a "Main" and a "Details" Activity. A user logs into Google Play Services in the "Main" Activity and I want to submit achievements and leaderboard data in the "Details" Activity.

I'm inheriting from BaseGameActivity in both Activities and using:

 mGoogleApiClient = getApiClient();

in "Details", however when I call isConnected it always returns false.

I even tried copying all the login/callback code from the "Main" Activity over, but it's still doesn't detect the user is logged in.

This post suggests not using BaseGameActivity and pass GameHelper using a singleton:

How to use BaseGameActivity.getApiClient() in multiple activities?

Not sure what the correct approach is.

解决方案

The most simple way to do this is to have both activities create a separate instance of the api client. The state of the connection is shared between them internally, so you don't need worry about how to pass around the client and handle callbacks that may happen when an activity is not active, and the player will only log in on your main activity.

Extending your activity from BaseGameActivity really is not needed any longer (for an entertaining explanation watch: Death of BasegameActivity. What you do need to do is implement the two interfaces that handle initializing the GoogleAPIClient:

public class MainActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
        }

To implement these, refer to the samples and the doc: https://developers.google.com/games/services/android/init

这篇关于坚持在活动之间登录Google Play服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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