簡體   English   中英

如何在 LIBGDX android 應用程序中獲取上下文()

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM