【问题标题】:How to check whether Google Play Games account available on Android device?如何检查 Android 设备上是否有可用的 Google Play 游戏帐户?
【发布时间】:2016-05-20 01:20:29
【问题描述】:

我想知道在 Android 设备上是否有可用的帐户注册为 Google Play 游戏帐户。

我正在使用 Java 和 LibGDX。

有什么想法吗?

【问题讨论】:

    标签: java android libgdx google-play-services google-play-games


    【解决方案1】:

    编辑: Google Play 游戏不再需要 here 中提到的 Google+ 帐户

    问题:要求不必要的范围

     // Don’t do it this way!  
     GoogleApiClient gac = new GoogleApiClient.Builder(this, this, this)  
               .addApi(Games.API)  
               .addScope(Plus.SCOPE_PLUS_LOGIN) // The bad part  
               .build();  
     // Don’t do it this way!  
    

    在这种情况下,开发人员专门请求 plus.login 范围。如果您要求 plus.login,您的用户将获得同意 对话框。

    解决方案:只询问您需要的范围

    // This way you won’t get a consent screen  
     GoogleApiClient gac = new GoogleApiClient.Builder(this, this, this)  
               .addApi(Games.API)  
               .build();  
     // This way you won’t get a consent screen  
    

    从您的 GoogleApiClient 构造中删除任何不需要的范围 以及您不再使用的任何 API。


    原答案:

    当用户使用herehere 提到的Google+ 帐户登录时,Google Play 游戏 帐户可用,并且该帐户本身可用如果用户使用 Google 帐户登录,那么我们的第一种方法是:

    1. 检查是否有 Google 帐户登录
    2. 检查该用户是否登录 Google+。

    要检查 Android 设备是否有 Google 用户登录

    public boolean isLoggedInGoogle() {
        AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
        Account[] list = manager.getAccounts();
    
        for (Account account : list) {
            if (account.type.equalsIgnoreCase("com.google")) {
                return true;
            }
        }
    
        return false;
    }
    

    现在,检查 Android 设备是否有 Google+ 用户登录

    您可以尝试使用 Google API 客户端启动登录序列,如 question 中所述:

    private GoogleApiClient buildGoogleApiClient() {
        return new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API, null)
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .build();
    }
    

    您也可以只使用Play Games Services APIsBaseGameUtilsthis link 有几个实现方法的示例。

    【讨论】:

    • 非常感谢您的详细解答。我确实使用 BaseGameUtils。但是,如果用户确实拥有 Google+,但尚未登录,会发生什么?我还有第二个问题:当用户还没有 Google+ 帐户时,不启动 Google Play 游戏是否有意义?我认为这可能会吓跑不喜欢使用 Google+ 的用户。
    • @Z0q 这些都是非常好的后续问题!所以我发现一篇 Android 开发者的帖子说 Google+ 不再需要了。我已经用信息编辑了我的答案。 android-developers.blogspot.com/2016/01/…
    • @Z0q 并且...如果您只是想删除签名逻辑或修改,我建议您检查这个问题:stackoverflow.com/questions/22046530/…
    • 谢谢。这对我来说很清楚。但是,我的问题是I would like to know if there is an account available on an Android device which is registered as a Google Play Games account.。您告诉我如何查看该帐户是否为 Google 帐户,但我如何知道它是否也注册了 Google Play 游戏?
    • 哦...你是对的,我什至没有通过编辑回答原始问题。所以......我要做的是:尝试在后台登录:博客文章提到“提示玩家每个帐户登录一次,而不是每个游戏一次”。因此,如果有一个帐户已经登录。你会从服务器收到一些东西。
    猜你喜欢
    • 1970-01-01
    • 2014-02-15
    • 2017-08-25
    • 2014-05-30
    • 2019-06-09
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 2018-01-31
    相关资源
    最近更新 更多