我不明白Google Play游戏服务是如何运作的?

时间:2014-11-06 18:42:25

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

我曾尝试关注Google Play服务的Google文档,但这是不可能的,因为Google文档是有史以来最糟糕的。

无论如何,我管理设置项目并在我的应用中添加Google Play服务,但我不知道如何在用户更改活动时保持播放器的连接状态。

我有 MainActivity ,其中扩展BaseGameActivity ,然后用户点击按钮Play,我打开 GameActivity ,在那里玩游戏,当我完成游戏时,我想将分数提交给Google Play服务排行榜,但我不能,因为我似乎没有连接。

我使用BaseGameActivity扩展MyActivity

 public class MainActivity extends BaseGameActivity

然后我在 GameActivity 上扩展了这个:

public GameActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener

然后在 onCreate 上使用此:

  // Create the Google Api Client with access to Plus and Games
   mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                    // add other APIs and scopes here as needed
            .build();

然后当我完成游戏时,我尝试提交分数,但似乎我没有连接。

  if(mGoogleApiClient.isConnected()){
                Games.Achievements.unlock(mGoogleApiClient,
                        getString(R.string.app_name));
                Games.Leaderboards.submitScore(mGoogleApiClient,
                        getString(R.string.number_guesses_leaderboard),
                        clickCounter);
            }

2 个答案:

答案 0 :(得分:1)

根据docs,您还需要致电mGoogleApiClient.connect()。他们建议您使用按钮进行登录,但您也可以在onStart()

进行登录
@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

如果一切顺利,您将收到已经实施的回调onConnected(Bundle connectionHint)

答案 1 :(得分:1)

Play游戏服务真的是以碎片为主题。几乎必须使用Fragments而不是Activities,并在您的应用程序中使用一个Activity ...