java - 如何检查 Android 设备上的 Google Play 游戏帐户是否可用?

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

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

我正在使用 Java 和 LibGDX。

有什么想法吗?

最佳答案

编辑: Google Play 游戏不再需要上述的 Google+ 帐户 here

问题:要求不必要的范围

 // 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!  

In this case, the developer is specifically requesting the plus.login scope. If you ask for plus.login, your users will get a consent dialog.

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

// 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  

Remove any unneeded scopes from your GoogleApiClient construction along with any APIs you no longer use.


原答案:

当用户使用提到的 Google+ 帐户登录时,Google Play 游戏 帐户可用 herehere ,如果用户使用 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();
}

您也可以只使用 BaseGameUtilsPlay Games Services APIs . this link 中有一些如何实现它的示例.

关于java - 如何检查 Android 设备上的 Google Play 游戏帐户是否可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35302514/

相关文章:

android - 扩展类 com.google.android.gms.ads.AdView Android Studio 时出错

java - 根据值获取数组项的索引

Android Jetpack Navigation 在 Fragment 之间传递 Lambda/Delegate

java - 如何在执行 'Unsupported class file major version 55' 时修复 'org.apache.spark.sql.DataSet.collectAsList()'

android - Android 上的标准除外

java - LibGDX : Dividing audio file

android - Android 上的 GdxRuntimeException : couldn't load shared library 'gdx' for target

java - LibGDX 按钮在触摸时没有响应

java - 是否可以使用 Spring 配置创建集合?

java - 编写简洁优雅的Java的技巧