关于 Android Studio 上的 Google Play 游戏服务

Posted

技术标签:

【中文标题】关于 Android Studio 上的 Google Play 游戏服务【英文标题】:About Google Play Games Services on Android Studio 【发布时间】:2017-08-03 23:38:35 【问题描述】:

我正在尝试在我的项目中添加排行榜和成就系统大约 3 天...

我可以登录我的应用,但是当我尝试打开排行榜时,它会说:

    E/UncaughtException: java.lang.IllegalStateException: GoogleApiClient is not configured to use the Games Api. Pass Games.API into GoogleApiClient.Builder#addApi() to use this feature.
                                                                                                      at com.google.android.gms.common.internal.zzac.zza(Unknown Source)
                                                                                                      at com.google.android.gms.games.Games.zzc(Unknown Source)
                                                                                                      at com.google.android.gms.games.Games.zzb(Unknown Source)
                                                                                                      at com.google.android.gms.games.Games.zzi(Unknown Source)
                                                                                                      at com.google.android.gms.games.internal.api.LeaderboardsImpl.getLeaderboardIntent(Unknown Source)
                                                                                                      at com.google.android.gms.games.internal.api.LeaderboardsImpl.getLeaderboardIntent(Unknown Source)
                                                                                                      at com.google.android.gms.games.internal.api.LeaderboardsImpl.getLeaderboardIntent(Unknown Source)
                                                                                                      at tr.com.blogspot.etkinlikhavuzu.benimilkogretmenim.Ilksayfa.onClick(Ilksayfa.java:309)
                                                                                                      at android.view.View.performClick(View.java:5702)
                                                                                                      at android.widget.TextView.performClick(TextView.java:10888)
                                                                                                      at android.view.View$PerformClick.run(View.java:22541)
                                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                      at android.os.Looper.loop(Looper.java:158)
                                                                                                      at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我该如何解决这个问题?

我写了这段代码:

            case R.id.Enbasarili:
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(mGoogleApiClient,
                    getResources().getString(R.string.leaderboard_en_cok_70_ve_ustu_basari_gosterenler)), RC_UNUSED);
            break;

感谢您的帮助...

【问题讨论】:

好吧,GoogleApiClient is not configured to use the Games Api.Pass Games.API into GoogleApiClient.Builder#addApi() to use this feature了吗? 我想我没有。我不明白该怎么做。 也许你应该去阅读一些文档,比如this。 我真的在网上搜索了大约一天的时间,但搜索错误的位置。无论如何谢谢你。我想我明白了。 【参考方案1】:

尝试检查这些文档 - Accessing the Play Games Services APIs in Your Android Game 和 Get Started with Play Games Services for Android

获取 Google Api 客户端并连接

您的游戏必须引用 GoogleApiClient 对象才能对 Google Play 游戏服务进行任何 API 调用。使用 GoogleApiClient.Builder 类在 onCreate() 中创建 GoogleApiClient 对象。例如:

private GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);

    // Create the Google Api Client with access to the Play Games services
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            // add other APIs and scopes here as needed
            .build();

    // ...

为了让您的游戏与 Google Play 游戏服务进行通信,您的客户端必须首先建立连接。一旦您的游戏不再需要访问这些服务,您可以断开客户端连接。

编辑:

Implementing Sign-in in Your Android Game

为了使用 Google Play 游戏服务,您的游戏需要实现用户登录,以通过 Google Play 游戏服务验证您的玩家身份。如果用户未通过身份验证,您的游戏在调用 Google Play 游戏服务 API 时会遇到错误。

private static int RC_SIGN_IN = 9001;

private boolean mResolvingConnectionFailure = false;
private boolean mAutoStartSignInflow = true;
private boolean mSignInClicked = false;

// ...

@Override
public void onConnectionFailed(ConnectionResult connectionResult) 
    if (mResolvingConnectionFailure) 
        // Already resolving
        return;
    

    // If the sign in button was clicked or if auto sign-in is enabled,
    // launch the sign-in flow
    if (mSignInClicked || mAutoStartSignInFlow) 
        mAutoStartSignInFlow = false;
        mSignInClicked = false;
        mResolvingConnectionFailure = true;

        // Attempt to resolve the connection failure using BaseGameUtils.
        // The R.string.signin_other_error value should reference a generic
        // error string in your strings.xml file, such as "There was
        // an issue with sign in, please try again later."
        if (!BaseGameUtils.resolveConnectionFailure(this,
                mGoogleApiClient, connectionResult,
                RC_SIGN_IN, getString(R.string.signin_other_error))) 
            mResolvingConnectionFailure = false;
        
    

    // Put code here to display the sign-in button

请参阅sample 以更好地了解 Google Play 游戏的代码实现。

希望这会有所帮助。

【讨论】:

感谢您的评论。但是当我昨天添加这个 ode 时它说: onConnectionFailed:ConnectionResultstatusCode=SIGN_IN_REQUIRED, resolution=PendingIntentf1c8656: android.os.BinderProxy@3392f9b, message=null 我使用了这个 ONcreate:mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Games.API) .addScope(Games.SCOPE_GAMES) .build(); mGoogleApiClient.connect(); 是的,您的游戏需要实现用户登录,以通过 Google Play 游戏服务验证您的玩家身份。否则,您在调用 Google Play 游戏服务 API 时会遇到错误。

以上是关于关于 Android Studio 上的 Google Play 游戏服务的主要内容,如果未能解决你的问题,请参考以下文章

Android studio 添加admob googgle play services

Android Studio教程从入门到精通

Android Studio 上的向后兼容性 (AppCompat)

AS WIFI 无线调试

AS WIFI 无线调试

Android Studio 2.2 中的 Espresso 测试录制功能