Google Sign-In with LibGDX(使用 LibGDX 进行 Google 登录)
问题描述
我有问题.我正在用 LibGDX 制作游戏.现在我想实现谷歌登录.我到处搜索,但找不到任何东西.
I've a problem. I'm making a game with LibGDX. Now I want to implement Google Sign-In. I searched everywhere, but can't find anything.
我需要一个解析器来抽象特定平台的代码,但我不知道该怎么做.有人可以帮忙吗?
What I need is a Resolver to abstract code for specific platform, but I don't know how to do it. Can someone help?
编辑
这是代码,这是我的 Android 解析器:
Here's the code, this is my Android Resolver:
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 中调用解析器方法的类
and this is my class that call the resolver method in 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());
}
});
推荐答案
我为我的游戏解决这个问题的方法是使用 接口.因此,您必须编写 Android 和 iOS 特定代码,在核心游戏中使用此接口.
The way I solved this for my game was with the use of Interfacing. So you have to write Android and iOS specific code that use this interfaces from your core game.
这篇关于使用 LibGDX 进行 Google 登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 LibGDX 进行 Google 登录
基础教程推荐
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01