仅在用户登录时如何执行此代码

时间:2019-05-11 23:42:42

标签: java android google-play-services google-play-games

我一直在尝试添加此代码以在游戏结束后更新玩家的得分,当用户登录时,它可以按我希望的方式工作,但是如果用户未登录,则游戏会崩溃

void game_over() {
    num_lifes = 0;
    findViewById(R.id.hero).setEnabled(false);
    findViewById(R.id.btn_play).setVisibility(View.GONE);
    show_lifes();
    Games.getLeaderboardsClient(this, GoogleSignIn.getLastSignedInAccount(this))
            .submitScore(getString(R.string.leaderboard_id), score);

    findViewById(R.id.game_over).setVisibility(View.VISIBLE);

1 个答案:

答案 0 :(得分:0)

假设您的User实例中有一种功能或便捷方法可以检查您将要使用的登录名

void game_over() {
    if (isSignedIn) {
        num_lifes = 0;
        findViewById(R.id.hero).setEnabled(false);
        findViewById(R.id.btn_play).setVisibility(View.GONE);
        show_lifes();
        Games.getLeaderboardsClient(this, GoogleSignIn.getLastSignedInAccount(this))
            .submitScore(getString(R.string.leaderboard_id), score);

        findViewById(R.id.game_over).setVisibility(View.VISIBLE);
    }
}

您的isSignedIn方法将如下所示

private boolean isSignedIn() {
  return GoogleSignIn.getLastSignedInAccount(context) != null;
}
相关问题