为排行榜和成就使用 Google Play 服务
Using Google Play Services for Leaderboards and Acheivements
我正在使用最新的 android SDK 为 android 市场构建应用程序。我需要登录才能为我的游戏排行榜和成就玩服务。
我有两个活动。我在第一个 activity 加载它播放服务,如果在第二个 activity 连接就可以访问它,我相信。
使用我当前的代码,它尝试连接到 google 播放服务,但被处理程序捕获,导致它尝试永远循环。注释部分,它尝试登录一次然后停止。这就是我要发布的 activity:
这是我的 Android 清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:androd="http://schemas.android.com/apk/res-auto"
package="com.my.application.name"
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- Add the following meta-data for devices running Google Play service. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<meta-data
android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
此外,这是我的初始屏幕:
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;
import com.google.android.gms.plus.Plus;
public class openingScreen extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener {
/* Request code used to invoke sign in user interactions. */
private static final int RC_SIGN_IN = 10004;
private boolean mResolvingConnectionFailure = false;
private boolean mAutoStartSignInflow = true;
private boolean mSignInClicked = false;
/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;
/* A flag indicating that a PendingIntent is in progress and prevents
* us from starting further intents.
*/
private boolean mIntentInProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opening_screen);
// Create the Google Api Client with access to Plus and Games
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();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
Games.signOut(mGoogleApiClient);
mGoogleApiClient.disconnect();
}
}
...
@Override
public void onConnected(Bundle bundle) {
Toast.makeText(getApplicationContext(), "Welcome Back, To the Stage of History ", Toast.LENGTH_SHORT).show();
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.sign_in_button) {
mGoogleApiClient.connect();
} else if (view.getId() == R.id.sign_out_button) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!mIntentInProgress && result.hasResolution()) {
try {
mIntentInProgress = true;
result.startResolutionForResult(this, // your activity
RC_SIGN_IN);
} catch (IntentSender.SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
Log.e("network", "onConnectionFailed: "+ String.valueOf(e));
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnectionSuspended(int cause) {
mGoogleApiClient.connect();
}
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
if (requestCode == RC_SIGN_IN) {
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
Log.e("network", "forever connecting loop Failed: " + requestCode + " "+ intent);
// mGoogleApiClient.connect();
}
}
}
}
记住,我还做了以下事情:
- 在 google 游戏中设置游戏服务排行榜和成就
开发者控制台。
- 确保匹配我的包裹名称的字符串
像我的应用程序到我的示例的反向域的句点
游戏。
- 链接游戏也在Google开发者控制台中
上面推荐制作游戏-ids.xml文件并放置我的
创建字符串。
- 使用生成的签名 apk 构建游戏,该 apk 使用单独的密钥库上传到 android - 我在终端中使用 Keytool 命令生成并输入应用程序的 sha1 密钥。
该应用尝试登录 google 播放服务,然后触发 activityResult() 回调:
查看我过去的 logcat 文件,跟踪显示 403 错误 Google 游戏服务未配置。正如我们从我的代码中看到的那样,堆栈给出了客户端 ID 未找到但它在游戏中的响应-ids.xml 就像 Google 玩 api 文档说的那样!
我已经选择这个太久了,这似乎无关紧要,因为我很小心只输入了为生产发布创建的 apk 生成的 sha1 密钥。堆栈溢出,请帮助。
07-26 17:18:48.224 5126-4861/? E/SignInIntentService﹕ Access Not Configured. The API (Google Play Game Services API) is not enabled for your project. Please use the Google Developers Console to update your configuration.
com.google.android.gms.games.server.error.GamesException
at com.google.android.gms.games.server.GamesServer.getResponseBlocking(GamesServer.java:156)
at com.google.android.gms.games.broker.PlayerAgent.getPlayerFromNetwork(PlayerAgent.java:1737)
at com.google.android.gms.games.broker.PlayerAgent.fetchPlayer(PlayerAgent.java:623)
at com.google.android.gms.games.broker.DataBroker.loadSelf(DataBroker.java:828)
at com.google.android.gms.games.service.PlayGamesSignInIntentService$LoadSelfOperation.executeInternal(PlayGamesSignInIntentService.java:366)
at com.google.android.gms.games.service.PlayGamesSignInIntentService$BaseOperation.execute(PlayGamesSignInIntentService.java:52)
at com.google.android.gms.games.service.PlayGamesSignInIntentService$OperationAdapter.execute(PlayGamesSignInIntentService.java:451)
at com.google.android.gms.chimera.BaseAsyncOperationService$OperationTask.run(BaseAsyncOperationService.java:179)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
07-26 17:18:48.234 32016-32016/? E/LoadSelfFragment﹕ Unable to sign in - application does not have a registered client ID
罪魁祸首是我被迫只加载它的签名版本。我无法使用在我的设备上生成的开发人员版本。使用 android 文件传输并使用应用程序设置菜单手动卸载我的设备让我可以安装签名版本。安装签名版本并将其上传到 Play 开发者控制台后,我似乎可以登录了!
我也意识到我的做法是徒劳的。事实证明,我似乎无法像我曾经想的那样将我的联系从 activity 转移到 activity。我现在正在重构我的代码以处理活动,因为我似乎无法正确处理 visibility:gone 或 visible: click 事件。
我正在使用最新的 android SDK 为 android 市场构建应用程序。我需要登录才能为我的游戏排行榜和成就玩服务。 我有两个活动。我在第一个 activity 加载它播放服务,如果在第二个 activity 连接就可以访问它,我相信。
使用我当前的代码,它尝试连接到 google 播放服务,但被处理程序捕获,导致它尝试永远循环。注释部分,它尝试登录一次然后停止。这就是我要发布的 activity:
这是我的 Android 清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:androd="http://schemas.android.com/apk/res-auto"
package="com.my.application.name"
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- Add the following meta-data for devices running Google Play service. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<meta-data
android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
此外,这是我的初始屏幕:
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;
import com.google.android.gms.plus.Plus;
public class openingScreen extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener {
/* Request code used to invoke sign in user interactions. */
private static final int RC_SIGN_IN = 10004;
private boolean mResolvingConnectionFailure = false;
private boolean mAutoStartSignInflow = true;
private boolean mSignInClicked = false;
/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;
/* A flag indicating that a PendingIntent is in progress and prevents
* us from starting further intents.
*/
private boolean mIntentInProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opening_screen);
// Create the Google Api Client with access to Plus and Games
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();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
Games.signOut(mGoogleApiClient);
mGoogleApiClient.disconnect();
}
}
...
@Override
public void onConnected(Bundle bundle) {
Toast.makeText(getApplicationContext(), "Welcome Back, To the Stage of History ", Toast.LENGTH_SHORT).show();
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.sign_in_button) {
mGoogleApiClient.connect();
} else if (view.getId() == R.id.sign_out_button) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!mIntentInProgress && result.hasResolution()) {
try {
mIntentInProgress = true;
result.startResolutionForResult(this, // your activity
RC_SIGN_IN);
} catch (IntentSender.SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
Log.e("network", "onConnectionFailed: "+ String.valueOf(e));
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnectionSuspended(int cause) {
mGoogleApiClient.connect();
}
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
if (requestCode == RC_SIGN_IN) {
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
Log.e("network", "forever connecting loop Failed: " + requestCode + " "+ intent);
// mGoogleApiClient.connect();
}
}
}
}
记住,我还做了以下事情:
- 在 google 游戏中设置游戏服务排行榜和成就 开发者控制台。
- 确保匹配我的包裹名称的字符串 像我的应用程序到我的示例的反向域的句点 游戏。
- 链接游戏也在Google开发者控制台中 上面推荐制作游戏-ids.xml文件并放置我的 创建字符串。
- 使用生成的签名 apk 构建游戏,该 apk 使用单独的密钥库上传到 android - 我在终端中使用 Keytool 命令生成并输入应用程序的 sha1 密钥。
该应用尝试登录 google 播放服务,然后触发 activityResult() 回调:
查看我过去的 logcat 文件,跟踪显示 403 错误 Google 游戏服务未配置。正如我们从我的代码中看到的那样,堆栈给出了客户端 ID 未找到但它在游戏中的响应-ids.xml 就像 Google 玩 api 文档说的那样!
我已经选择这个太久了,这似乎无关紧要,因为我很小心只输入了为生产发布创建的 apk 生成的 sha1 密钥。堆栈溢出,请帮助。
07-26 17:18:48.224 5126-4861/? E/SignInIntentService﹕ Access Not Configured. The API (Google Play Game Services API) is not enabled for your project. Please use the Google Developers Console to update your configuration. com.google.android.gms.games.server.error.GamesException at com.google.android.gms.games.server.GamesServer.getResponseBlocking(GamesServer.java:156) at com.google.android.gms.games.broker.PlayerAgent.getPlayerFromNetwork(PlayerAgent.java:1737) at com.google.android.gms.games.broker.PlayerAgent.fetchPlayer(PlayerAgent.java:623) at com.google.android.gms.games.broker.DataBroker.loadSelf(DataBroker.java:828) at com.google.android.gms.games.service.PlayGamesSignInIntentService$LoadSelfOperation.executeInternal(PlayGamesSignInIntentService.java:366) at com.google.android.gms.games.service.PlayGamesSignInIntentService$BaseOperation.execute(PlayGamesSignInIntentService.java:52) at com.google.android.gms.games.service.PlayGamesSignInIntentService$OperationAdapter.execute(PlayGamesSignInIntentService.java:451) at com.google.android.gms.chimera.BaseAsyncOperationService$OperationTask.run(BaseAsyncOperationService.java:179) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) at java.lang.Thread.run(Thread.java:841) 07-26 17:18:48.234 32016-32016/? E/LoadSelfFragment﹕ Unable to sign in - application does not have a registered client ID
罪魁祸首是我被迫只加载它的签名版本。我无法使用在我的设备上生成的开发人员版本。使用 android 文件传输并使用应用程序设置菜单手动卸载我的设备让我可以安装签名版本。安装签名版本并将其上传到 Play 开发者控制台后,我似乎可以登录了!
我也意识到我的做法是徒劳的。事实证明,我似乎无法像我曾经想的那样将我的联系从 activity 转移到 activity。我现在正在重构我的代码以处理活动,因为我似乎无法正确处理 visibility:gone 或 visible: click 事件。