Google 排行榜提交分数不起作用

Google leaderboard submitting score doesn't work

你能解释一下为什么命令 Games.Leaderboards.submitScore(mGoogleApiClient, leaderboard_id, score) 不起作用吗?当我在 Alpha 测试中上传已签名的 apk 时:登录有效,加载排行榜也有效,但排行榜不显示任何分数。

这是我的 MainActivity,包括所有 Google 服务...

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);
        }
    }
}

我使用了两个排行榜,所以它可能与只使用一个排行榜有很大不同。

感谢您的回复

排行榜 ID 有问题,我将其存储在 strings.xml 中,我称它们为 String.valueOf(R.string.id),但 returns 错误。所以我只是手动将那个字符串写在了“”中。