【发布时间】:2018-12-14 02:11:21
【问题描述】:
我正在尝试在我的 Android 应用中实现自动玩家登录到 Google Play 游戏。首先,如here所述,我尝试静默登录:
@Override
protected void onResume() {
super.onResume();
signInSilently();
}
private void signInSilently() {
mGoogleSignInClient.silentSignIn().addOnCompleteListener(this, task -> {
if (task.isSuccessful())
//everything ok
else {
final ApiException exception = (ApiException) task.getException();
if (BuildConfig.DEBUG)
Log.d(TAG, "Silent Sign In failure: ", exception);
if (exception.getStatusCode() == CommonStatusCodes.SIGN_IN_REQUIRED)
startSignInIntent();
}
});
每次我遇到代码 4 (CommonStatusCodes.SIGN_IN_REQUIRED) 的异常时。所以在这种情况下,我尝试使用 ui 登录:
private void startSignInIntent() {
startActivityForResult(mGoogleSignInClient.getSignInIntent(), RC_SIGN_IN);
}
@Override
protected void onActivityResult(int request, int response, Intent data) {
super.onActivityResult(request, response, data);
if (request == RC_SIGN_IN) {
final GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// everything is ok, get account from result
} else if (result.getStatus().hasResolution()) {
resolveManually(result.getStatus());
} else {
String message = result.getStatus().getStatusMessage();
if (BuildConfig.DEBUG)
Log.d(TAG, "status code" + result.getStatus().getStatusCode());
if (message == null || message.isEmpty()) {
message = "other error";
}
new AlertDialog.Builder(this).setMessage(message)
.setNeutralButton(android.R.string.ok, null).show();
}
}
}
每次我收到带有其他错误的消息时都在这里!状态代码再次为 4 (CommonStatusCodes.SIGN_IN_REQUIRED)。当我尝试使用 Intent 登录时如何获取此代码?因此,我的应用程序处于无限循环中,因为每次我的活动在收到结果后加载时都会调用 onResume,并且每次状态代码都是 CommonStatusCodes.SIGN_IN_REQUIRED。那么,问题出在哪里? 在 Google samples 中没有信息如何处理自动登录,只有手动登录按钮。但谷歌recommends 使用自动登录。请帮助任何人了解这里出了什么问题。
【问题讨论】:
标签: java android google-signin google-play-games