防止应用程序被操作系统停止或暂停

问题描述:

我正在开发CodenameOne上的Android应用程序,即使最小化,它也需要每5分钟发送一次网络请求.我如何实现此行为,以防止请求被操作系统停止或暂停?

I'm developing and Android application on CodenameOne that needs to send a web request every 5 minutes even when minimized. How can I achieve this behavior in order to prevent that the request get stopped or paused by the OS?

其他人的回答是正确的,您需要创建服务,但他们以某种方式忽略了对Codename One的提及.

The other guys answers are correct that you need to create a service but they somehow ignored the mention of Codename One.

在代号下,您需要为android创建一个本机目录,然后将服务源代码放在此处(只需使用一个空白的服务类,它实际上并没有做任何事情).然后,您需要在构建参数中添加选项android.xapplication,以在其中声明服务XML属性.

Under Codename One you need to create a native directory for android and just place the service source code there (just use a blank service class that doesn't really do anything). Then you need to add to the build arguments the option android.xapplication where you would state the service XML attributes.

已经说过,您尝试做的事情是非常错误的,您不应该在Android中这样做!您将立即耗尽设备的电池寿命,并且该服务最终将被操作系统终止(因为这将浪费电池).解决方案是向设备发送推送通知以唤醒应用程序.

Having said that what you are trying to do is VERY wrong and you shouldn't do it in Android! You will drain the battery life from the device in no time and the service will be killed by the OS eventually (since it will be a battery drain). The solution is to send a push notification to the device to wake up the application.