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

时间:2017-03-13 19:59:17

标签: android android-studio google-api google-play-games google-api-client

我试图在我的项目上添加排行榜和竞争系统大约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;

感谢您的帮助......

1 个答案:

答案 0 :(得分:0)

请尝试查看这些文档 - Accessing the Play Games Services APIs in Your Android GameGet Started with Play Games Services for Android

  

获取Google Api客户端并连接

     

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

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游戏的代码实施。

希望这有帮助。