在过去的三天里,我一直在尝试将Google Game Services添加到我的LibGDX项目中,起初我尝试了LibGDX教程,但它们似乎都已过时.然后建议我使用Google Game Services官方代码
LibGDX: How to Implement Google Play Game Services?
我导入了示例项目TypeANumber,并尝试将代码添加到我的项目中,但是当我尝试登录时,出现“ signInSilently():失败”错误,当我尝试打开排行榜和成就时,该错误崩溃调试并签名APKS.
这是我的代码:
AndroidLauncher:
private void signInSilently() {
Log.d(TAG, "signInSilently()");
mGoogleSignInClient.silentSignIn().addOnCompleteListener(this, new OnCompleteListener
}
@Override
public void showLeaderboards(){
runOnUiThread(new Runnable() {
public void run() {onShowLeaderboardsRequested();
}
});
}
@Override
public void showAchievements(){
runOnUiThread(new Runnable() {
public void run() {onShowAchievementsRequested();
}
});
}
表现:
android:value="@string/app_id" />
android:value="@integer/google_play_services_version"/>
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/GdxTheme" >
android:cOnfigChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
android:label="@string/app_name"
android:screenOrientation="portrait"
android:cOnfigChanges="keyboard|keyboardHidden|orientation|screenSize">
在我的build.gradle中,我添加了:
compile "com.google.android.gms:play-services-games:11.8.0"
compile "com.google.android.gms:play-services-auth:11.8.0"
Logcat:
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()
解决方法:
从问题的第一眼看,似乎您将元数据保留在application标签之外,但应放在AndroidManifest.xml文件中的application标签内
像这样 :
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/GdxTheme" >
android:cOnfigChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
android:label="@string/app_name"
android:screenOrientation="portrait"
android:cOnfigChanges="keyboard|keyboardHidden|orientation|screenSize">
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
});
}
private void onConnected(GoogleSignInAccount googleSignInAccount) {
mLeaderboardsClient = Games.getLeaderboardsClient(this, googleSignInAccount);
mPlayersClient = Games.getPlayersClient(this, googleSignInAccount);
mPlayersClient.getCurrentPlayer()
.addOnCompleteListener(new OnCompleteListener
});
}
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
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
});
}
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
});
}
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 MyServices{
void startSignInIntent();
boolean isSignedIn();
void showLeaderBoard();
void submitScore(int score);
}
用户安装应用程序后,第一次由我自己调用接口的startSignInIntent().