有没有人有关于在SQLite中从Android上的libGDX存储数据的任何提示。我对Android SDK中使用的技术非常熟悉,但是我不知道如何从libGDX调用这些Android数据库函数。我知道从libGDX调用函数会使我的游戏无法在桌面上使用,但是我可以解决这个问题。
一种方法总是在您的主项目中创建一个界面,我们称它为NativeFunctions。然后,让桌面和Android应用程序/活动都实现此界面。在创建主项目时,您将传递应用程序/活动。在您的主应用程序中,您保留对传递的接口的引用,并使用此接口来调用本机函数,您可以分别为台式机和Android实现本机功能(不会使游戏在台式机上无法使用,也可以在其中使用SQLite;)。
NativeFunctions
好的,那很复杂,所以让我们来看一下它的作用(定义一个打开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实现:
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 } }
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.