我有一个问题。我正在用 LibGDX 制作游戏。现在我想实现Google登录。我到处寻找,但什么也没找到。
我需要的是一个解析器来抽象特定平台的代码,但我不知道该怎么做。有人可以帮忙吗?
编辑
这是代码, 这是我的 Android 解析器:
public GoogleResolverAndroid(final Context context) {
handler = new Handler();
this.context = context;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this.context)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
public void loginGoogle() {
signIn();
}
@Override
public boolean getIsLoggedInGoogle() {
return isLoggedIn;
}
public void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
((AndroidLauncher)context).startActivityForResult(signInIntent, RC_SIGN_IN);
mGoogleApiClient.connect();
}
private void signOut() {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
isLoggedIn = false;
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
if (requestCode == REQUEST_RESOLVE_ERROR) {
mResolvingError = false;
if (resultCode == RESULT_OK) {
// Make sure the app is not already connected or attempting to connect
if (!mGoogleApiClient.isConnecting() &&
!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
}
}
//some code other code
//
//
//
@Override
public void onConnected(@Nullable Bundle bundle) {
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone())
{
Gdx.app.debug(TAG, "Loggato");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
handleSignInResult(googleSignInResult);
}
});
}
}
@Override
public void onConnectionSuspended(int i) {
Gdx.app.debug(TAG, "onConnectionSuspended ma non so perchè");
}
这是我的类,调用 libgdx 中的解析器方法
// Google
googleLoginButton = new LoginButton(tbs, stage, main);
googleLoginButton.setPosition(stage.getViewport().getWorldWidth()/2-googleLoginButton.getWidth() - 10,
stage.getViewport().getWorldHeight()/2-googleLoginButton.getHeight()/2 - 200);
googleLoginButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
main.googleResolver.loginGoogle();
googlePrefs = main.googleResolver.getGooglePrefs();
gLoginIn = true;
Gdx.app.debug(TAG, googlePrefs.toString());
}
});
最佳答案
我在游戏中解决这个问题的方法是使用 Interfacing 。所以你必须写Android and iOS使用核心游戏中的此接口(interface)的特定代码。
关于java - 使用 LibGDX 登录 Google,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39536189/