在Android上使用SQLite从libGDXAndroid、SQLite、libGDX

2023-09-05 06:14:36 作者:热烈到崩溃

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

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.

推荐答案

一种方法是始终在您的主项目创建一个接口,我们称之为 NativeFunctions 。然后,让您的桌面和Android应用程序/活动实现此接口。在创建您的主项目,你一路传递应用程序/活动。在主应用程序,你保持一个引用传递接口并使用它来调用本机的功能,它可以为台式机和Android单独实现(不使桌面上你的游戏无法使用,你可以使用SQLite有作为;)。

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 ;).

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

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

接口:

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

主类:

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");
    }
}

Android的实现:

The Android implementation:

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
    }
}

就是这样,你的实现使用SQLite也许应该沿着相同的道路。顺便说一句。我想,这也是整合的广告箱和交谈的系统一般的方式。

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.