简体   繁体   English

在Android上使用libGDX中的SQLite

[英]Using SQLite from libGDX on Android

Does anyone have any tips on storing data from libGDX on Android in SQLite. 有没有人有任何关于在SQLite中在Android上存储libGDX数据的提示。 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. 我非常熟悉Android SDK中使用的技术,但我不知道如何从libGDX调用这些Android数据库函数。 I know that calling the functions from libGDX will make my game unusable on the desktop but I can deal with that. 我知道从libGDX调用函数会使我的游戏在桌面上无法使用,但我可以处理它。

One approach is always to create an interface in your main project, let's call it NativeFunctions . 一种方法是始终在主项目中创建一个接口,让我们称之为NativeFunctions You then let both your desktop and your Android application/activity implement this interface. 然后,您的桌面和Android应用程序/活动都会实现此界面。 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 ;). 在您的主应用程序中,您保留对传递的接口的引用,并使用它来调用本机函数,您可以单独为桌面和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. 就是这样,使用SQLite的实现可能应该是一样的。 Btw. 顺便说一句。 I think that's also the way to integrate advertisement boxes and to talk to the system in general. 我认为这也是整合广告盒和与系统交谈的方式。

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

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