Prevent that the app get stopped or paused by the OS

我的未来我决定 提交于 2019-12-13 04:31:03

问题


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?


回答1:


You cant do that from the activity, you'll need to create background service.

http://developer.android.com/training/run-background-service/create-service.html




回答2:


Use AlarmManager to set up your every-five-minute operation. Have it trigger a BroadcastReceiver, which in turn passes control to my WakefulIntentService (or your own IntentService that handles the WakeLock, that you will need so that the device stays awake while you do your work). Have the service do the "web request".

This is still not 100% guaranteed:

  • The user can Force Stop you from Settings, in which case your alarms are gone and nothing will happen until the user manually runs your app again

  • The user might block alarms from firing on certain devices, like various SONY Xperia models, that offer "stamina mode" or the equivalent

However, it is the best that you are going to get, short of rolling your own Android OS version.




回答3:


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

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.

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.



来源:https://stackoverflow.com/questions/17602514/prevent-that-the-app-get-stopped-or-paused-by-the-os

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!