在Android上使用libGDX中的SQLite

时间:2021-02-25 13:34:01

Does anyone have any tips on storing data from libGDX on Android in SQLite. I am very familiar with the techniques used in the Android SDK but I don't have any idea how to call those Android database functions from libGDX. I know that calling the functions from libGDX will make my game unusable on the desktop but I can deal with that.

有没有人有关于在SQLite中在Android上存储来自libGDX的数据的任何提示。我非常熟悉Android SDK中使用的技术,但我不知道如何从libGDX调用这些Android数据库函数。我知道从libGDX调用函数会使我的游戏在桌面上无法使用,但我可以处理它。

1 个解决方案

#1


12  

One approach is always to create an interface in your main project, let's call it NativeFunctions. You then let both your desktop and your Android application/activity implement this interface. On creation of your main project you pass the application/activity along. In your main application you keep a reference to the passed interface and use this to call native functions, which you can implement for desktop and Android separately (not making your game unusable on the desktop, you can use SQLite there as well ;).

一种方法总是在主项目中创建一个接口,让我们称之为NativeFunctions。然后,让桌面和Android应用程序/活动都实现此界面。在创建主项目时,您将传递应用程序/活动。在您的主应用程序中,您保留对传递的接口的引用,并使用它来调用本机函数,您可以单独为桌面和Android实现(不会使您的游戏在桌面上无法使用,您也可以在那里使用SQLite;)。

Ok, that was complicated, so let's see it in action (defining a function to open an URL):

好的,这很复杂,所以让我们看看它在行动中(定义一个打开URL的函数):

The interface:

界面:

public interface NativeFunctions {
    public void openURL(String url);
}

The main class:

主要课程:

public class MyGame extends Game/ApplicationListener {
    public NativeFunctions mNativeFunctions;

    public MyGame(NativeFunctions nativeFunctions) {
        mNativeFunctions = nativeFunctions;
    }
    // Exemplary function call, of course this doesn't make sense in render() ;)
    public void render() {
        mNativeFunctions.openURL("http://www.example.com");
    }
}

The Android implementation:

Android实现:

public class MyGameActivity extends AndroidApplication implements NativeFunctions {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initialize(new MyGame(this), false);
    }
    public void openURL(String url) {
        Intent viewIntent = new Intent("android.intent.action.VIEW", 
            Uri.parse(url));
        startActivity(viewIntent);  
    }
}

The desktop implementation:

桌面实现:

public class MyGameDesktop implements NativeFunctions {
    public static void main(String[] args) {
        MyGameDesktop game = new MyGameDesktop();
        new LwjglApplication(new MyGame(game), "MyGame", 800,
             480, false);
    }
    public void openURL(String url) {
        // Your implementation to open URL on dekstop
    }
}

That's it, your implementation to use SQLite should probably be along the same way. Btw. I think that's also the way to integrate advertisement boxes and to talk to the system in general.

就是这样,使用SQLite的实现可能应该是一样的。顺便说一句。我认为这也是整合广告盒和与系统交谈的方式。

#1


12  

One approach is always to create an interface in your main project, let's call it NativeFunctions. You then let both your desktop and your Android application/activity implement this interface. On creation of your main project you pass the application/activity along. In your main application you keep a reference to the passed interface and use this to call native functions, which you can implement for desktop and Android separately (not making your game unusable on the desktop, you can use SQLite there as well ;).

一种方法总是在主项目中创建一个接口,让我们称之为NativeFunctions。然后,让桌面和Android应用程序/活动都实现此界面。在创建主项目时,您将传递应用程序/活动。在您的主应用程序中,您保留对传递的接口的引用,并使用它来调用本机函数,您可以单独为桌面和Android实现(不会使您的游戏在桌面上无法使用,您也可以在那里使用SQLite;)。

Ok, that was complicated, so let's see it in action (defining a function to open an URL):

好的,这很复杂,所以让我们看看它在行动中(定义一个打开URL的函数):

The interface:

界面:

public interface NativeFunctions {
    public void openURL(String url);
}

The main class:

主要课程:

public class MyGame extends Game/ApplicationListener {
    public NativeFunctions mNativeFunctions;

    public MyGame(NativeFunctions nativeFunctions) {
        mNativeFunctions = nativeFunctions;
    }
    // Exemplary function call, of course this doesn't make sense in render() ;)
    public void render() {
        mNativeFunctions.openURL("http://www.example.com");
    }
}

The Android implementation:

Android实现:

public class MyGameActivity extends AndroidApplication implements NativeFunctions {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initialize(new MyGame(this), false);
    }
    public void openURL(String url) {
        Intent viewIntent = new Intent("android.intent.action.VIEW", 
            Uri.parse(url));
        startActivity(viewIntent);  
    }
}

The desktop implementation:

桌面实现:

public class MyGameDesktop implements NativeFunctions {
    public static void main(String[] args) {
        MyGameDesktop game = new MyGameDesktop();
        new LwjglApplication(new MyGame(game), "MyGame", 800,
             480, false);
    }
    public void openURL(String url) {
        // Your implementation to open URL on dekstop
    }
}

That's it, your implementation to use SQLite should probably be along the same way. Btw. I think that's also the way to integrate advertisement boxes and to talk to the system in general.

就是这样,使用SQLite的实现可能应该是一样的。顺便说一句。我认为这也是整合广告盒和与系统交谈的方式。