AndroidでGoogle Play Game Servicesにログインします。
※BaseGameActivityを継承せず、GameHelperを使用する版です。
【準備】
1.「Google Play services SDKをワークスペースにインポートする方法」で「Google Play services SDK」をワークスペースにインポートしておきます。
2.「Google Play Game ServicesのBaseGameUtilsライブラリをワークスペースにインポートする方法」の手順で、BaseGameUtilsライブラリをワークスペースにインポートしておきます。
3.「Google Play Developer Consoleでゲームサービスを作成する方法」の手順で、ゲームサービスを作成しておきます。
4.「Google Play Developer Consoleのゲームサービスにテスト用IDを追加する方法」の手順で、テスト用のGoogleIDを追加しておきます。
5.「Google Play Developer Consoleのゲームサービスにアプリをリンクする方法」の手順で、ゲームサービスにアプリをリンクしておきます。
※アプリのパッケージ名は「jp.blogspot.foolprogrammer.googleplaygameservicessample01_002」で行いました。
【手順1】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの作成手順で、「GooglePlayGameServicesSample01-002-Login」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
| 項目名 | 項目に設定する値 |
| アプリケーション名(Application Name) | jp.blogspot.foolprogrammer.googleplaygameservicessample01_002 |
| プロジェクト名(Project Name) | GooglePlayGameServicesSample01-002-Login |
| パッケージ名(Package Name) | jp.blogspot.foolprogrammer.googleplaygameservicessample01_002 |
| Build SDK | API 10 |
| Minimum Required SDK | API 10 |
2.「Google Play services SDKをライブラリ参照する方法」の手順で、Google Play services SDKをライブラリ設定する。
3.「Google Play Game ServicesのBaseGameUtilsをライブラリ参照する方法」の手順で、BaseGameUtilsをライブラリ設定する。
【手順2】
1.「AndroidManifest.xml」は以下の通り。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jp.blogspot.foolprogrammer.googleplaygameservicessample01_002"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="googleplaygameservicessample"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="jp.blogspot.foolprogrammer.googleplaygameservicessample01_002.MainActivity"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順3】
1.「res/layout/activity_main.xml」は以下の通り。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.gms.common.SignInButton
android:id="@+id/button_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ログアウト"
android:textSize="12sp" />
</LinearLayout>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順4】
1.「src/jp/blogspot/foolprogrammer/googleplaygameservicessample01_002/MainActivity.java」は以下の通り。
package jp.blogspot.foolprogrammer.googleplaygameservicessample01_002;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.google.android.gms.games.Games;
import com.google.android.gms.games.Player;
import com.google.example.games.basegameutils.GameHelper;
import com.google.example.games.basegameutils.GameHelper.GameHelperListener;
public class MainActivity extends Activity implements GameHelperListener, OnClickListener {
private GameHelper gameHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
this.gameHelper.setup(this);
findViewById(R.id.sign_in_button).setOnClickListener(this);
findViewById(R.id.sign_out_button).setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
this.gameHelper.onStart(this);
}
@Override
protected void onStop() {
super.onStop();
this.gameHelper.onStop();
}
@Override
protected void onActivityResult(int request, int response, Intent data) {
super.onActivityResult(request, response, data);
this.gameHelper.onActivityResult(request, response, data);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.sign_in_button:
this.gameHelper.beginUserInitiatedSignIn();
break;
case R.id.sign_out_button:
onClickSignOut();
break;
}
}
@Override
public void onSignInSucceeded() {
changeUiState(true);
Player player = Games.Players.getCurrentPlayer(this.gameHelper.getApiClient());
String playerName;
if (player == null) {
playerName = "???";
} else {
playerName = player.getDisplayName();
}
((TextView) findViewById(R.id.name)).setText("こんにちは、『" + playerName + "』さん。");
}
@Override
public void onSignInFailed() {
changeUiState(false);
}
public void onClickSignOut() {
this.gameHelper.signOut();
changeUiState(false);
}
private void changeUiState(boolean isLogin) {
if (isLogin) {
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);
} else {
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.sign_out_button).setVisibility(View.GONE);
((TextView) findViewById(R.id.name)).setText("こんにちは『???』さん。");
}
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順5】
1.「任意のファイルの作成方法」の手順で「res/values/ids.xml」を作成。
2.「res/values/ids.xml」は以下の通り。
<resources>
<string name="app_id">【あなたのAPP_ID】</string>
</resources>
2.【あなたのAPP_ID】は【準備】の1で作成したゲームサービスのIDを入力します。3.「Ctrl+Shift+F」を押し、コードをフォーマッティング。
4.「Ctrl+S」を押し、ファイルを保存。
【手順6】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの実行手順で、実行。
※エミュレーターでは「Google Play services SDK」が上手く動かないみたいで、実機で実行する必要があるみたいです。
【手順7】
1.初回実行時には以下のダイアログが表示されるので「ログイン」をタップ。
3.ログインが成功すれば、以下の様に表示されます。
4.ログアウトを押すとログアウトします。
以上です。




0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。