Error in Google Leaderboard View: GoogleApiClient must be connected

Getting a problem while saving a score on the leaderboard.

In my case, the first thing on the login screen is the user who is using Google. The second screen is game activity and after the game is over I want the score to be saved to the leaderboard.

I am using below code to initialize GoogleApiClient: -

mGoogleApiClient = new GoogleApiClient.Builder(this)

        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(Games.API).addScope(Games.SCOPE_GAMES)
        .build();
setContentView(R.layout.activity_game);

      

To save a game account: -

 Games.Leaderboards.submitScore(mGoogleApiClient, getResources().getString(R.string.leaderboard_id), millis);

      

But it says: - GoogleApiClient must be connected.

But when add api sign to google client like below, it says you cannot use Games.Api with Sign_in_api.

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this , this )
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .build();

      

Basically my question is how can I connect the GoogleApiClient in my active activity so that I can use Games.Leaderboards.submitScore to save my game score.

Any help would be appreciated.

+3


source to share


1 answer


If you are using BaseGameActivity

, expanding your activity is no longer required. Instead, use a library BaseGameUtils

to resolve connection errors and display dialogs.

Try to initialize GoogleApiClient by implementing interfaces GoogleApiClient.ConnectionCallbacks

and GoogleApiClient.OnConnectionFailedListener

. See the documentation for more information.

import com.google.android.gms.*;
import com.google.example.games.basegameutils.BaseGameUtils;

public class MyGameActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

      



For more information, you can check the following:

0


source







All Articles