theMatus theMatus - 9 months ago 75
Android Question

Google leaderboard submitting score doesn't work

Can you explane me why command

Games.Leaderboards.submitScore(mGoogleApiClient, leaderboard_id, score)
doesn't work? When I upload signed apk on Alpha testing: signing in works, loading leaderboard also works, but the leaderboard doesn't show any scores.

This is my MainActivity including all Google Services...

imports...

public class MainActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener {

FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
FrameLayout fragmentView;

int actualFragment;

public boolean accountConnected;

com.google.android.gms.common.SignInButton signIn;
Button signOut;

private GoogleApiClient mGoogleApiClient;

private static int RC_SIGN_IN = 9001;

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SharedPreferences prefs = getSharedPreferences("com.thematus.twinz", MODE_PRIVATE);

accountConnected = prefs.getBoolean("accountIsConnected", false);
// 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)
.build();

fragmentView = (FrameLayout) findViewById(R.id.fragmentView);

mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragmentView, new MenuFragment()).commit();
actualFragment = 0;

signIn = (com.google.android.gms.common.SignInButton) findViewById(R.id.sign_in_button);
signIn.setOnClickListener(this);
signOut = (Button) findViewById(R.id.sign_out_button);
signOut.setOnClickListener(this);
}

public void loadLeaderboard() {
if(accountConnected) {
Intent leaderboard = new Intent(Games.Leaderboards.getAllLeaderboardsIntent(mGoogleApiClient));
startActivityForResult(leaderboard, 2);
}
}

public void loadClassicHighscore() {
if (accountConnected) {
Games.Leaderboards.loadCurrentPlayerLeaderboardScore(mGoogleApiClient, getString(R.string.classic_leaderboard_id), LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
@Override
public void onResult(final Leaderboards.LoadPlayerScoreResult scoreResult) {
if (isScoreResultValid(scoreResult)) {
SharedPreferences prefs = getSharedPreferences("com.thematus.twinz", MODE_PRIVATE);
prefs.edit().putInt("classic_highscore", (int) scoreResult.getScore().getRawScore()).apply();
}
}
});
}
}

public void loadZenHighscore() {
if (mGoogleApiClient.isConnected()) {
Games.Leaderboards.loadCurrentPlayerLeaderboardScore(mGoogleApiClient, getString(R.string.zen_leaderboard_id), LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
@Override
public void onResult(final Leaderboards.LoadPlayerScoreResult scoreResult) {
if (isScoreResultValid(scoreResult)) {
SharedPreferences prefs = getSharedPreferences("com.thematus.twinz", MODE_PRIVATE);
prefs.edit().putInt("zen_highscore", (int) scoreResult.getScore().getRawScore()).apply();
}
}
});
}
}

private boolean isScoreResultValid(final Leaderboards.LoadPlayerScoreResult scoreResult) {
return scoreResult != null && GamesStatusCodes.STATUS_OK == scoreResult.getStatus().getStatusCode() && scoreResult.getScore() != null;
}

public void submitClassicHighscore(long score) {
if(mGoogleApiClient.isConnected()) {
Games.Leaderboards.submitScore(mGoogleApiClient, String.valueOf(R.string.classic_leaderboard_id), score);
Games.Leaderboards.submitScoreImmediate(mGoogleApiClient, String.valueOf(R.string.classic_leaderboard_id), score);
} else {
Toast.makeText(this, "unable to submit highscore", Toast.LENGTH_SHORT).show();
}
}

public void submitZenHighscore(long score) {
if(mGoogleApiClient.isConnected()) {
Games.Leaderboards.submitScore(mGoogleApiClient, String.valueOf(R.string.zen_leaderboard_id), score);
} else {
Toast.makeText(this, "unable to submit highscore", Toast.LENGTH_SHORT).show();
}
}

// Call when the sign-in button is clicked
private void signInClicked() {
mSignInClicked = true;
mGoogleApiClient.connect();
}

// Call when the sign-out button is clicked
private void signOutclicked() {
mSignInClicked = false;
Games.signOut(mGoogleApiClient);
}

@Override
protected void onStart() {
super.onStart();
SharedPreferences prefs = getSharedPreferences("com.thematus.twinz", MODE_PRIVATE);
if (prefs.getBoolean("accountIsConnected", false) == true) {
mGoogleApiClient.connect();
accountConnected = true;
prefs.edit().putBoolean("accountIsConnected", accountConnected).apply();
} else {
accountConnected = false;
prefs.edit().putBoolean("accountIsConnected", accountConnected).apply();
}
}

@Override
protected void onStop() {
SharedPreferences prefs = getSharedPreferences("com.thematus.twinz", MODE_PRIVATE);
prefs.edit().putBoolean("accountIsConnected", accountConnected).apply();
super.onStop();
mGoogleApiClient.disconnect();
}

@Override
public void onConnected(Bundle bundle) {
// show sign-out button, hide the sign-in button
signIn.setVisibility(View.GONE);
signOut.setVisibility(View.VISIBLE);

accountConnected = true;
loadClassicHighscore();
loadZenHighscore();
SharedPreferences prefs = getSharedPreferences("com.thematus.twinz", MODE_PRIVATE);
submitClassicHighscore(prefs.getInt("classic_highscore", 0));
submitZenHighscore(prefs.getInt("zen_highscore", 0));
prefs.edit().putBoolean("accountIsConnected", accountConnected).apply();

// (your code here: update UI, enable functionality that depends on sign in, etc)
}

@Override
public void onConnectionSuspended(int i) {
// Attempt to reconnect
mGoogleApiClient.connect();
}

@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, "Something went wrong!")) {
mResolvingConnectionFailure = false;
}
}

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

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == RC_SIGN_IN) {
mSignInClicked = false;
mResolvingConnectionFailure = false;
if (resultCode == RESULT_OK) {
mGoogleApiClient.connect();
} else {
accountConnected = false;
SharedPreferences prefs = getSharedPreferences("com.thematus.twinz", MODE_PRIVATE);
prefs.edit().putBoolean("accountIsConnected", accountConnected).apply();
BaseGameUtils.showActivityResultError(this, requestCode, resultCode, R.string.signin_failure);
}
}
}

@Override
public void onClick(View v) {
if (v.getId() == R.id.sign_in_button) {
mSignInClicked = true;
mGoogleApiClient.connect();
}
else if (v.getId() == R.id.sign_out_button) {
// sign out.
mSignInClicked = false;
Games.signOut(mGoogleApiClient);

// show sign-in button, hide the sign-out button
signIn.setVisibility(View.VISIBLE);
signOut.setVisibility(View.GONE);
}
}
}


I'm using two leaderboards so it might be quite different than using only one.

Thanks for any reply

Answer Source

There was problem in leaderboard id, i have it stored in strings.xml and I was calling them like String.valueOf(R.string.id) which returns wrong. So I only manually wrote that string in "" which went.