首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Play服务不会提交分数

问Play服务不会提交分数
EN

Stack Overflow用户
提问于 2014-12-20 03:58:05
回答 2查看 166关注 0票数 0

在尝试向我的Play Services Leader板提交分数时,我遇到了一个问题。在我的MainActivity中,用户成功登录,然后从我的GameScreen活动中,我尝试提交分数,但失败了:(...My问题是连接是否持续,或者我是否必须在提交之前重新连接用户?

主要活动。

代码语言:javascript
复制
public class MainActivity extends BaseGameActivity implements View.OnClickListener,      GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

Button siButton;
private GoogleApiClient mGoogleApiClient;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .build();

    siButton = (Button) findViewById(R.id.sign_out_button);
    siButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (view.getId() == R.id.sign_in_button) {
                beginUserInitiatedSignIn();
            } else if (view.getId() == R.id.sign_out_button) {
                signOut();
                findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
                findViewById(R.id.sign_out_button).setVisibility(View.GONE);
            }
        }
    });    
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onSignInSucceeded() {
    findViewById(R.id.sign_in_button).setVisibility(View.GONE);
    findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);
}

@Override
public void onSignInFailed() {
    findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
    findViewById(R.id.sign_out_button).setVisibility(View.GONE);
}

@Override
public void onClick(View v) {

}

@Override
public void onConnected(Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

}

GAMESCREEN

代码语言:javascript
复制
public class GameScreen extends BaseGameActivity implements GoogleApiClient.ConnectionCallbacks,    GoogleApiClient.OnConnectionFailedListener {

private int c;
private GoogleApiClient mGoogleApiClient;
TextView counter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_screen);
    setRequestedClients(BaseGameActivity.CLIENT_GAMES | BaseGameActivity.CLIENT_APPSTATE);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .build();



private void progressSetup() {
    Thread timerThread = new
            Thread() {
                @Override
                public void run() {
                    try {
                        while (mbActive && (waited < TIMER_RUNTIME)) {
                            sleep(THREAD_SLEEP);
                            if (mbActive) {
                                waited += 100;
                                updateProgress(waited);
                                if (waited > 9999) {
                                    mbActive = false;
                                    gameOver();
                                }
                            }
                        }
                    } catch (InterruptedException e) {
                        Log.d("fail", "failure" + e);
                    }
                }
            };
    startTread = false;
    timerThread.start();
}

private void updateProgress(final int timePassed) {
    if (null != mProgressBar) {
        progress = mProgressBar.getMax() * timePassed / TIMER_RUNTIME;
        mProgressBar.setProgress(progress);
    }
}

//game over
private void gameOver() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {             
            upDateScore();
        }
    });
}

private void upDateScore(){
    if (mGoogleApiClient.isConnected()) {
        Games.Leaderboards.submitScore(mGoogleApiClient, getString(R.string.app_id), c);
        Log.d("connectted.....................................................");
    } else {          
        Log.d("not connectted.....................................................");
    }
}

@Override
public void onBackPressed() {
    Intent home = new Intent(GameScreen.this, MainActivity.class);
    home.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    startActivity(home);
}

@Override
public void onConnected(Bundle bundle) {
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}

@Override
public void onSignInFailed() {
}

@Override
public void onSignInSucceeded() {
}

}

EN

回答 2

Stack Overflow用户

发布于 2014-12-20 05:11:06

只需在method Games.Leaderboards.submitScore(mGoogleApiClient,getString(R.string.app_id)中将getString(R.string.app_id)替换为您的排行榜id即可。

排行榜id如下:“CgkI0tXt-q0HEA*”

票数 2
EN

Stack Overflow用户

发布于 2014-12-20 18:45:17

伊万·赛布罗是对的。请参见Leaderboards in Android - Updating the player's score,您必须使用LEADERBOARD_ID。

在开发人员控制台的游戏服务部分,您可以使用提供的按钮轻松地将排行榜和成就ids导出为xml文件。

通常,将游戏服务与多个活动一起使用可能会导致额外的等待时间,因为每个活动在开始时都会单独登录。因此,据我所知,使用片段的实现应该更好,参见这个著名的示例playgameservices/android-basic-samples。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27573077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档