简体   繁体   English

如何使用libGDX使我的应用程序在iOS / Android上在后台运行

[英]How can I keep my app running in the background on iOS/Android with libGDX

I'm using libGDX and I want to keep my application running even when it's closed to send push notifications/alerts to the user. 我正在使用libGDX,并且即使关闭应用程序以向用户发送推送通知/警报,也希望保持我的应用程序运行。 I need to do this for both iOS and Android. 我需要针对iOS和Android都执行此操作。

You will need to Implement a Service on android and create an interface for it. 您将需要在android上实现服务并为其创建接口。 Basically this is going to be platform dependent code for your game . 基本上,这将是您游戏的平台相关代码。

  1. Create an Interface AndroidPushNotificationServiceCallback; 创建一个接口AndroidPushNotificationServiceCallback;

     public interface AndroidPushNotificationServiceCallback { public void startService(); public void sendPushNotification(); public void stopService(); } 
  2. Then make your main GameListener constructor have this listener as a parameter 然后使您的主要GameListener构造函数将此侦听器作为参数

      public class MainGameListener extends ApplicationListener{ public MainGameListener(AndroidPushNotificationServiceCallback callback) this.callback = callback; } 
  3. Then make your android mainactivity implement this interface 然后让您的android mainactivity实现此接口

     public class MainActivity extends AndroidApplication implements AndroidPushNotificationServiceCallback{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration(); cfg.useGL20 = true; cfg.useAccelerometer=false; cfg.useCompass=false; initialize(new MainGameListener(), cfg); } public void startService(){ // start a service here } public void sendPushNotification(){ // send push notifcations here } public void stopService(){ // stop the service here } } 

Now in your gamelistener you need to call the method to start the service on the callback. 现在,在您的游戏监听器中,您需要调用方法以在回调上启动服务。 the callback implementation will then start the service. 然后,回调实现将启动服务。

This is just a dummy example . 这只是一个虚拟的例子。 Plus this approach is generic , use it in any situation you want to call platform specific methods. 加上这种方法是通用的,可在您要调用平台特定方法的任何情况下使用它。

Once the service is started, it can set alarms and other stuff to periodically invoke code (like sending push notification in your case) 服务启动后,它可以设置警报和其他内容以定期调用代码(例如在您的情况下发送推送通知)

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

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