1

I have a foreground service in a video calling app that keeps the network connection alive when the device screen locks. It is well known that foreground services can be killed by Android when the device is low on memory and/or battery power. Currently I am looking for reliable techniques to force Android to kill my foreground service.

This will let me test my solution, to allow the app to recover once the service gets killed. But I cannot seem to get android to kill my service.

I read here that only 3 foreground services can be kept running at any time. So I tried to run a music app, google maps and my video calling app together and have the device's screen turn off with all the associated foreground services running. But my service is still not being killed. I also tried running Youtube simultaneously to no effect. What else could I try?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • I'm not sure where you're getting that about only 3. I'm 99% sure that's wrong, and even if it isn't you couldn't trust it to be the same across OEMs and versions. But if you want your service killed, call stopService(). Or from the service call stopSelf() – Gabe Sechan Mar 26 '19 at 19:25
  • Thank you @GabeSechan. I see this sentence in the link provided in my question: `Only 3 service processes can nominate themselves as foreground services at any given time...`. Maybe it is incorrect or I misunderstood. But I think I could call `stopSelf()` after a fixed duration to simulate the service kill. However I think it will be different from an Android service kill since `onDestroy()` will be called in this case. – pendingIntent Mar 26 '19 at 20:10

0 Answers0