在 Android 上使用来自 libGDX 的 SQLite

Using SQLite from libGDX on Android(在 Android 上使用来自 libGDX 的 SQLite)

本文介绍了在 Android 上使用来自 libGDX 的 SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有任何关于在 SQLite 中在 Android 上存储来自 libGDX 的数据的提示.我非常熟悉 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);  
    }
}

桌面实现:

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.

这篇关于在 Android 上使用来自 libGDX 的 SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Android 上使用来自 libGDX 的 SQLite

基础教程推荐