【问题标题】:How to getContext() in LIBGDX android app如何在 LIBGDX android 应用程序中获取上下文()
【发布时间】:2020-02-02 12:01:26
【问题描述】:

我需要获取我的应用程序的Context,但在我的主类中从Game 扩展,所以我不能从Activity 扩展。有人知道怎么做吗?

谢谢!

【问题讨论】:

  • 请发布您到目前为止尝试过的内容

标签: android android-activity libgdx android-context


【解决方案1】:

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

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

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

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

【讨论】:

  • 这不起作用,因为接口需要在不依赖android的核心模块中,因此不知道上下文类型。相反,他可能希望在接口中有诸如getString 之类的方法,或者在android 模块实现中调用Context 的任何方法。
  • 接口是要走的路,但询问的人需要在接口中有一个方法来操作或使用Context 实例。这个接口需要在android模块上实现
  • 好吧,我可以使用getContext()method 创建一个Interfaz,但这与将类添加到Android 模块并从Android 导入Context 的库相同。问题是这些类没有上下文,所以我不能在任何地方使用Context context = this...我不知道我是否在任何时候弄错了...
  • 将我要发布的澄清评论变成完整的答案,希望对您有所帮助!
【解决方案2】:

接口是要走的路,因为您无法从 Core 模块访问 Android 特定代码。

第 1 步:创建界面(核心模块)

public interface MyInterface {

    void manipulateContext();

    void manipulateContextWithExtraParams(String example, int example2);
}

第 2 步:实现接口(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 步:存储和使用您在界面上定义的方法(CORE MODULE)

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 类中

【讨论】:

  • 非常感谢,成功了!他们的关键是访问AndroidLauncher 中的context,这是唯一扩展AndroidApplication 的类
【解决方案3】:

我试过的是:

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

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

任何其他方法都可以通过以下方式获得创建数据库的路径:

db = SQLiteDatabase.openOrCreateDatabase(path,null);

【讨论】:

  • 好吧,正如我在回答中指出的那样,您需要创建一个Interface。在您的原始帖子中,您没有提到数据库。但是,如果这是您的目标,那么 Interface 应该包含一个方法 getDatabase() 或类似的东西。在 Android 模块中实现Interface,方法是使用您在上面发布的代码和一些其他代码来操作它覆盖getDatabase() 方法。 LibGDX 不知道 SQLiteDatabase 是什么,所以你不能返回那个对象。在每个其他模块中,getDatabase() 应该返回 null。
  • 好吧,但是你如何覆盖getContext()方法?你在里面放什么?因为据我所知,如果您的主类从Application 类扩展,您只能访问context
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 2011-06-14
  • 2015-09-29
相关资源
最近更新 更多