简体   繁体   English

如何在 LIBGDX android 应用程序中获取上下文()

[英]How to getContext() in LIBGDX android app

I need to get the Context of my app but in my main Class extends from Game so I can not extends from Activity .我需要获取我的应用程序的Context但在我的主类中从Game扩展,所以我不能从Activity扩展。 Does anybody know how to do it?有人知道怎么做吗?

Thank you!谢谢!

LibGDX is a cross-platform game engine, so your application can be executed on multiple platforms. LibGDX 是一个跨平台的游戏引擎,因此您的应用程序可以在多个平台上执行。 Only Android, which is just one supported platform, can provide a Context object.只有 Android,它只是一个受支持的平台,可以提供Context对象。

To get around this issue, you'll need to create an Interface in the core module of your LibGDX project.要解决此问题,您需要在 LibGDX 项目的核心模块中创建一个Interface That Interface can, for example, contain a getContext() method.例如,该接口可以包含一个getContext()方法。 Add the interface as an argument in the constructor of your main LibGDX class.在主 LibGDX 类的构造函数中添加接口作为参数。 In every platform-specific module, you should then implement this Interface , override the getContext() method (by returning a Context object in the android module and null in every other module) and pass it with the constructor for the main LibGDX class in the Launcher class for that module.在每个特定于平台的模块中,你应该实现这个Interface ,覆盖getContext()方法(通过在 android 模块中返回一个Context对象并在每个其他模块中返回null )并将它与主 LibGDX 类中的构造函数一起传递该模块的启动器类。

For more information about the topic, read the LibGDX Wiki: https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code有关该主题的更多信息,请阅读 LibGDX Wiki: https : //github.com/libgdx/libgdx/wiki/Interface-with-platform-specific-code

EDIT: LibGDX isn't able to handle the Context object, you'll need to manipulate the Context object in the Android module, instead of passing it to the core module!编辑: LibGDX 无法处理Context对象,您需要在 Android 模块中操作Context对象,而不是将其传递给核心模块! Thanks to @Nicolas and @Luis Fernando Frontanilla for mentioning this.感谢@Nicolas 和@Luis Fernando Frontanilla 提到这一点。

Interfacing is the way to go since you can't access Android specific code from Core module.接口是要走的路,因为您无法从Core模块访问Android特定代码。

Step 1: Create the interface (CORE MODULE)第 1 步:创建接口(核心模块)

public interface MyInterface {

    void manipulateContext();

    void manipulateContextWithExtraParams(String example, int example2);
}

Step 2: Implement the interface (ANDROID MODULE)第二步:实现接口(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);
        }
    }
}

Step 3: Send the implemented interface your game (ANDROID MODULE)第 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);
    }
}

Step 4: Store and use the methods you defined on your interface (CORE MODULE)第 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);
    }
}

As you can see, you will not be manipulating the Context from the core module directly, instead, place that logic on the InterfaceImplementation class如您所见,您不会直接从核心模块操作Context ,而是将该逻辑放在InterfaceImplementation类上

What I have tried is:我尝试过的是:

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

but I don't have any context to feed that method.但我没有任何上下文来提供该方法。

It would be usefull any other way to get a path to create the db with:获取创建数据库的路径的任何其他方式都会很有用:

db = SQLiteDatabase.openOrCreateDatabase(path,null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM