1
我第一次试图实现谷歌播放服务的领导者板,但有一些问题。在多个活动中连接GoogleApiClient的问题
我在主要活动中显示排行榜按钮,所以我在MainActivity中初始化一个GoogleApiClient,目前为止都很好 - 谷歌连接和空白排行榜。
实际填满排行榜的分数是从第二项活动GameActivity中获得的。 我创建了另一个GoogleApiClient实例并尝试连接它。 但它..它总是失败,即使在调用apiClient.connect()后
我做错了什么?整个计划只允许1个客户端?
我使用此代码在两个活动的初始化:
apiClient = new GoogleApiClient.Builder(this)
.addApi(Games.API)
.addScope(Games.SCOPE_GAMES)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(LevelSelector.this, "Failed to connect to google play", Toast.LENGTH_LONG).show();
}
})
.build();
我打电话排行榜从主要活动
leaderboardTextView = (TextView) findViewById(R.id.leaderboard_button);
leaderboardTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(
Games.Leaderboards.getLeaderboardIntent(apiClient,
getString(R.string.leaderboard_most_stars)), 0);
}
});
而且分数是根据不同的活动提交到排行榜
private void updateScore(){
if(!apiClient.isConnected()){
apiClient.connect();
}
//Always returns false.
if(apiClient.isConnected()) {
Games.Leaderboards.submitScore(apiClient,
getString(R.string.leaderboard_most_stars),
totalStars);
}
}