![](/img/trans.png)
[英]How to share libgdx preference file between multiple android libgdx app
[英]How to getContext() in LIBGDX android app
我需要获取我的应用程序的Context
但在我的主类中从Game
扩展,所以我不能从Activity
扩展。 有人知道怎么做吗?
谢谢!
LibGDX 是一个跨平台的游戏引擎,因此您的应用程序可以在多个平台上执行。 只有 Android,它只是一个受支持的平台,可以提供Context
对象。
要解决此问题,您需要在 LibGDX 项目的核心模块中创建一个Interface
。 例如,该接口可以包含一个getContext()
方法。 在主 LibGDX 类的构造函数中添加接口作为参数。 在每个特定于平台的模块中,你应该实现这个Interface
,覆盖getContext()
方法(通过在 android 模块中返回一个Context
对象并在每个其他模块中返回null
)并将它与主 LibGDX 类中的构造函数一起传递该模块的启动器类。
有关该主题的更多信息,请阅读 LibGDX Wiki: https : //github.com/libgdx/libgdx/wiki/Interface-with-platform-specific-code
编辑: LibGDX 无法处理Context
对象,您需要在 Android 模块中操作Context
对象,而不是将其传递给核心模块! 感谢@Nicolas 和@Luis Fernando Frontanilla 提到这一点。
接口是要走的路,因为您无法从Core模块访问Android特定代码。
第 1 步:创建接口(核心模块)
public interface MyInterface {
void manipulateContext();
void manipulateContextWithExtraParams(String example, int example2);
}
第二步:实现接口(ANDROID MODULE)
import android.content.Context;
public class InterfaceImplementation implements MyInterface {
private Context context;
public InterfaceImplementation(Context context) {
// Store the context for later use
this.context = context;
}
@Override
public void manipulateContext() {
// Do something with the context, this is called on the core module
System.out.println(context);
}
@Override
public void manipulateContextWithExtraParams(String example, int example2) {
if (example2 == 1) {
System.out.println(example + context);
} else {
System.out.println(example);
}
}
}
第 3 步:将实现的界面发送到您的游戏(ANDROID MODULE)
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.frontanilla.helping.getcontext.InterfaceImplementation;
import com.frontanilla.helping.getcontext.MyGame;
public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
InterfaceImplementation interfaceImplementation = new InterfaceImplementation(this);
// Here we send the implementation to our Game in Core module
initialize(new MyGame(interfaceImplementation), config);
}
}
第 4 步:存储和使用您在接口上定义的方法(核心模块)
import com.badlogic.gdx.Game;
public class MyGame extends Game {
private MyInterface myInterface;
public MyGame(MyInterface myInterface) {
// Store for later use
this.myInterface = myInterface;
}
@Override
public void create() {
// Example of manipulating the Android Context indirectly from Core module
myInterface.manipulateContext();
myInterface.manipulateContextWithExtraParams("Hello", 2);
}
}
如您所见,您不会直接从核心模块操作Context
,而是将该逻辑放在InterfaceImplementation
类上
我尝试过的是:
scoreHelper = new ScoreSQLiteHelper(context,"dbtest",null,1); db = scoreHelper.getWritableDatabase();
但我没有任何上下文来提供该方法。
获取创建数据库的路径的任何其他方式都会很有用:
db = SQLiteDatabase.openOrCreateDatabase(path,null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.