はじめに
リーダーボードの閲覧と、スコア送信を実装します。
プロジェクトは前回のものを使用します。
リーダーボードIDの定義
リソースファイル「ids.xml」にリーダーボードIDを定義します。
ids.xml
xml version="1.0" encoding="utf-8"
<resources>
<string name="app_id">YOUR_APP_ID</string>
<string name="lb_id">YOUR_LB_ID</string>
</resources>
YOUR_LB_IDに先ほど取得したリーダーボードIDを指定します。
レイアウトファイル
前回のレイアウトファイルに、リーダーボード見るボタンとスコア送信ボタンを追加します。
activity_main.xml
<LinearLayout xmlnsandroid="http://schemas.android.com/apk/res/android"
androidlayout_width="match_parent"
androidlayout_height="match_parent"
androidorientation="vertical" >
<comgoogleandroidgmscommonSignInButton
androidid="@+id/button_sign_in"
androidlayout_width="wrap_content"
androidlayout_height="wrap_content" />
<Button
androidid="@+id/button_sign_out"
androidlayout_width="wrap_content"
androidlayout_height="wrap_content"
androidtext="Sign Out" />
<Button
androidid="@+id/button_show_lb"
androidlayout_width="wrap_content"
androidlayout_height="wrap_content"
androidtext="リーダーボード見る" />
<Button
androidid="@+id/button_send_score"
androidlayout_width="wrap_content"
androidlayout_height="wrap_content"
androidtext="スコア送信" />
</LinearLayout>
Activityの実装
MainActivity.java
package jp.kforce.samplegame;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.google.android.gms.common.SignInButton;
import com.google.example.games.basegameutils.BaseGameActivity;
public class MainActivity extends BaseGameActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SignInButton buttonSignIn = (SignInButton) findViewById(R.id.button_sign_in);
buttonSignIn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.v("tag", "buttonSignIn click");
beginUserInitiatedSignIn();
}
});
final Button buttonSignOut = (Button) findViewById(R.id.button_sign_out);
buttonSignOut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.v("tag", "buttonSignOut click");
signOut();
}
});
final Button buttonShowLB = (Button) findViewById(R.id.button_show_lb);
buttonShowLB.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.v("tag", "buttonShowLB click");
if (isSignedIn()) {
startActivityForResult(getGamesClient().getAllLeaderboardsIntent(), 5001);
} else {
showAlert("Please sign in to view leaderboards.");
}
}
});
final Button buttonSendScore = (Button) findViewById(R.id.button_send_score);
buttonSendScore.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.v("tag", "buttonSendScore click");
if (isSignedIn()) {
int score = (int)(Math.random() * 100);
getGamesClient().submitScore(getString(R.string.lb_id), score);
showAlert("send : "+score);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onSignInSucceeded() {
Log.v("tag", "onSignInSucceeded");
}
@Override
public void onSignInFailed() {
Log.v("tag", "onSignInFailed");
}
}
今回も非常に簡単です。
前回からの追加点は「リーダーボード見るボタンのクリックリスナー」と「スコア送信ボタンのクリックリスナー」のみです。
リーダーボードを見るには
startActivityForResult(getGamesClient().getAllLeaderboardsIntent(), 5001);
とするだけです。
また、スコア送信は
getGamesClient().submitScore(getString(R.string.lb_id), score);
とするだけです。
おわりに
「Google Play Game Servicesを試してみたい」の連載はこれで終了です。
あとは気になったことがあれば単発で載せると思います。
ありがとうございました。