Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: keep web server running

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?

like image 875
Dennis Thrysøe Avatar asked Oct 28 '25 04:10

Dennis Thrysøe


1 Answers

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);
like image 64
artkoenig Avatar answered Oct 30 '25 08:10

artkoenig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!