【问题标题】:I don't understand how Google Play Games Services work?我不明白 Google Play 游戏服务的工作原理?
【发布时间】:2015-01-03 09:43:15
【问题描述】:

我曾尝试遵循 Google Play Services 的 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);
            }

【问题讨论】:

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


    【解决方案1】:

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

    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }
    

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

    【讨论】:

      【解决方案2】:

      Play 游戏服务真正考虑到了片段。几乎必须使用 Fragments 而不是 Activity,并且在您的应用中只有一个 Activity...

      【讨论】:

      猜你喜欢
      • 2018-09-11
      • 2014-02-02
      • 2014-09-17
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2014-01-19
      • 2013-07-31
      相关资源
      最近更新 更多