如何在 LIBGDX android 应用程序中使用 getContext()

How to getContext() in LIBGDX android app

我需要获取我的应用程序的 Context,但在我的主要 Class 中扩展自 Game,因此我无法扩展自 Activity。有人知道怎么做吗?

谢谢!

LibGDX 是一个跨平台的游戏引擎,因此您的应用程序可以在多个平台上执行。只有 Android,这只是一个受支持的平台,可以提供 Context 对象。

要解决此问题,您需要在 LibGDX 项目的核心模块中创建一个 Interface。例如,该接口可以包含一个 getContext() 方法。将接口作为参数添加到 main LibGDX class 的构造函数中。在每个特定于平台的模块中,您应该实现此 Interface ,覆盖 getContext() 方法(通过在 android 模块和 null 中返回一个 Context 对象在所有其他模块中)并将其与主 LibGDX class 的构造函数一起传递给该模块的 Launcher class。

有关该主题的更多信息,请阅读 LibGDX Wiki:https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code

编辑: LibGDX 无法处理 Context 对象,您需要在 Android 模块中操作 Context 对象,而不是将其传递给核心模块!感谢@Nicolas 和@Luis Fernando Frontanilla 提到这一点。

我试过的是:

scoreHelper = new ScoreSQLiteHelper(context,"dbtest",null,1); db = scoreHelper.getWritableDatabase();

但我没有任何上下文可以提供该方法。

获取创建数据库的路径的任何其他方式都会有用:

db = SQLiteDatabase.openOrCreateDatabase(path,null);

接口是可行的方法,因为您无法从 Core 模块访问 Android 特定代码。

第一步:创建接口(核心模块)

public interface MyInterface {

    void manipulateContext();

    void manipulateContextWithExtraParams(String example, int example2);
}

第二步:实现接口(ANDROID模块)

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 模块)

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 class