4

I am running a web server (NanoHTTPD) in an Android Service.

onStartCommand() returns the value from the overridden method in Service, so it should be "sticky".

However the service seems to stop, or at least the webserver stops responding. This happens mostly when the device has been inactive for a while, with the screen off. But also sometimes when it is seemingly running normally. The notification icon of the service keeps being displayed.

The device is not doing anything else, and it is plugged into power.

What can be done to keep the web server responding to requests? Or at least reduce the frequency of outages?

Dennis Thrysøe
  • 1,791
  • 4
  • 19
  • 31
  • "the service seems to stop" when? after a while? after serving a request? when device goes to sleep? – rupps Jun 01 '17 at 12:40
  • Question edited. It seems to stop responding mostly when in "sleep mode", but also sometimes when it is not. I am now trying START_STICKY instead of START_STICKY_COMPATIBILITY along with android:persistent="true" (which seems to be a nasty flag, but okay in my case, since the device will always be plugged in to power). – Dennis Thrysøe Jun 02 '17 at 13:41

1 Answers1

4

The best chances not being killed by the system in your case:

  • Start the service by startForeground
  • Prompt your users to include your app in the "ignore battery optimizations" list (you can not do this programmatically): Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); startActivity(intent);
artkoenig
  • 7,117
  • 2
  • 40
  • 61