gpt4 book ai didi

java - 尝试使用 LibGDX 登录 GooglePlay 游戏服务时出现 "signInSilently(): failure"

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:08 25 4
gpt4 key购买 nike

在过去 3 天里,我一直在尝试将 Google Game Services 添加到我的 LibGDX 项目中,起初我尝试了 LibGDX 教程,但它们似乎都已过时。然后我被建议使用谷歌游戏服务官方代码

LibGDX: How to Implement Google Play Game Services?

我导入了示例项目 TypeANumber并尝试将代码添加到我的项目中,但是当我尝试登录时,我收到了 "signInSilently(): failure"错误,当我尝试打开排行榜和成就时它崩溃了调试和签名的 APKS。

这是我的代码:

安卓启动器:

     private void signInSilently() {
Log.d(TAG, "signInSilently()");

mGoogleSignInClient.silentSignIn().addOnCompleteListener(this,
new OnCompleteListener<GoogleSignInAccount>() {
@Override
public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
if (task.isSuccessful()) {
Log.d(TAG, "signInSilently(): success");
onConnected(task.getResult());
} else {
Log.d(TAG, "signInSilently(): failure", task.getException());
onDisconnected();
}
}
});
}

@Override
public void showLeaderboards(){
runOnUiThread(new Runnable() {
public void run() {
onShowLeaderboardsRequested();
}
});
}

@Override
public void showAchievements(){
runOnUiThread(new Runnable() {
public void run() {
onShowAchievementsRequested();
}
});
}

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.harrybanda.blaster" >

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="27" />

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<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"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/GdxTheme" >

<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />

<activity
android:name="com.harrybanda.blaster.AndroidLauncher"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

在我的 build.gradle 中我添加了:

 compile "com.google.android.gms:play-services-games:11.8.0"
compile "com.google.android.gms:play-services-auth:11.8.0"

日志:

01-07 09:56:30.011 618-618/? D/Blaster: onResume()
01-07 09:56:30.011 618-618/? D/Blaster: signInSilently()
01-07 09:56:30.512 618-618/? D/Blaster: signInSilently(): failure
com.google.android.gms.common.api.b: 4:
at com.google.android.gms.common.internal.y.a(Unknown Source)
at com.google.android.gms.common.internal.ae.a(Unknown Source)
at com.google.android.gms.common.internal.af.a(Unknown Source)
at com.google.android.gms.common.api.internal.BasePendingResult.c(Unknown Source)
at com.google.android.gms.common.api.internal.BasePendingResult.a(Unknown Source)
at com.google.android.gms.auth.api.signin.internal.g.a(Unknown Source)
at com.google.android.gms.auth.api.signin.internal.r.onTransact(Unknown Source)
at android.os.Binder.execTransact(Binder.java:451)
01-07 09:56:30.512 618-618/? D/Blaster: onDisconnected()

最佳答案

从第 1st 看你的问题,你似乎将元数据保存在应用程序标签之外,但应该在 AndroidManifest.xml 文件

像这样:

  <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/GdxTheme" >

<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />

<activity
android:name="com.harrybanda.blaster.AndroidLauncher"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<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"/>
</application>

StatusCode 4 (SIGN_IN_REQUIRED) error means client attempted to connect to the service but the user is not signed in.


这是我的 AndroidLauncher

public class AndroidLauncher extends AndroidApplication implements MyServices {

private GoogleSignInClient mGoogleSignInClient;
private LeaderboardsClient mLeaderboardsClient;
private PlayersClient mPlayersClient;

private static final String TAG=AndroidLauncher.class.getSimpleName();

private static final int RC_SIGN_IN = 9001;
private static final int RC_UNUSED = 5001;
private static final int RC_LEADERBOARD_UI = 9004;

private String greetingMsg="Welcome, ";
private boolean greetingDisplayed;

@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

greetingDisplayed=false;
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new GdxGame(this), config);

mGoogleSignInClient = GoogleSignIn.getClient(this,
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build());

}

@Override
public boolean isSignedIn() {
return GoogleSignIn.getLastSignedInAccount(this) != null;
}

private void signInSilently() {

mGoogleSignInClient.silentSignIn().addOnCompleteListener(this,
new OnCompleteListener<GoogleSignInAccount>() {
@Override
public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
if (task.isSuccessful()) {
greetingMsg="Welcome back, ";
onConnected(task.getResult());
} else {
onDisconnected();
}
}
});
}

private void onConnected(GoogleSignInAccount googleSignInAccount) {

mLeaderboardsClient = Games.getLeaderboardsClient(this, googleSignInAccount);
mPlayersClient = Games.getPlayersClient(this, googleSignInAccount);

mPlayersClient.getCurrentPlayer()
.addOnCompleteListener(new OnCompleteListener<Player>() {
@Override
public void onComplete(@NonNull Task<Player> task) {
String displayName;
if (task.isSuccessful()) {
displayName = task.getResult().getDisplayName();
} else {
Exception e = task.getException();
handleException(e, getString(R.string.players_exception));
displayName = "???";
}

if(!greetingDisplayed)
welcomeMessage(displayName);
}
});
}

private void welcomeMessage(String name){

Toast toast = Toast.makeText(this, greetingMsg + name, Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 0, 0);
View view = toast.getView();
TextView text = (TextView) view.findViewById(android.R.id.message);
toast.show();
greetingDisplayed=true;
}

@Override
public void startSignInIntent() {
startActivityForResult(mGoogleSignInClient.getSignInIntent(), RC_SIGN_IN);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);

if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(intent);

try {
GoogleSignInAccount account = task.getResult(ApiException.class);
greetingMsg="Welcome, ";
onConnected(account);
} catch (ApiException apiException) {
String message = apiException.getMessage();
if (message == null || message.isEmpty()) {
message = getString(R.string.signin_other_error);
}

onDisconnected();
}
}
}

@Override
protected void onResume() {
super.onResume();
signInSilently();
}

private void signOut() {

if (!isSignedIn()) {
Log.w(TAG, "signOut() called, but was not signed in!");
return;
}

mGoogleSignInClient.signOut().addOnCompleteListener(this,
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
boolean successful = task.isSuccessful();
Log.d(TAG, "signOut(): " + (successful ? "success" : "failed"));

onDisconnected();
}
});
}

private void onDisconnected() {

mLeaderboardsClient = null;
mPlayersClient = null;
}

@Override
public void submitScore(int score){
if(isSignedIn())
mLeaderboardsClient.submitScore(getString(R.string.leaderboard_id), score);
}

@Override
public void showLeaderBoard() {
if(isSignedIn())
mLeaderboardsClient.getLeaderboardIntent(getString(R.string.leaderboard_id))
.addOnSuccessListener(new OnSuccessListener<Intent>() {
@Override
public void onSuccess(Intent intent) {
startActivityForResult(intent, RC_LEADERBOARD_UI);
}
});
}

private void handleException(Exception e, String details) {
int status = 0;

if (e instanceof ApiException) {
ApiException apiException = (ApiException) e;
status = apiException.getStatusCode();
}

String message = getString(R.string.status_exception_error, details, status, e);

new AlertDialog.Builder(this)
.setMessage(message)
.setNeutralButton(android.R.string.ok, null)
.show();
}
}

MyServices接口(interface)

interface MyServices{

void startSignInIntent();
boolean isSignedIn();
void showLeaderBoard();
void submitScore(int score);
}

用户安装app后第一次自己调用接口(interface)的startSignInIntent()

关于java - 尝试使用 LibGDX 登录 GooglePlay 游戏服务时出现 "signInSilently(): failure",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48135531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com