Android Foreground Services without an Activity

Viewed 650

I tried to find if there's a way to run foreground service (one which would hopefully never be killed) without any ui. (Ok I guess notification is necessary but other than that)

This is a very specific use-case since the device being used is a custom one (not a phone), where we need one 'server' app, and might be couple 'client' apps. Clients app will have all necessary ui, but server app should behave in a way like a web server.

I understand this is not a intention of foreground services, but it is justified in the use-case we have.

Bonus question: Is there a 'best' way to achieve an android process/service absolutely constantly running and never being killed by platform for cleaning the memory, since this service will be de facto critical part of the system. Like a phone/dial app on phones for example

1 Answers

Sorry, I can't write comments so I have to post an answer.

It's not exactly what you are looking for, but maybe this google codelab can help you start with something:

https://codelabs.developers.google.com/codelabs/while-in-use-location/#0

The code in the sample project starts a foreground service whenever the app leaves foreground, allowing the service to "survive" even if the application it's destroyed. Basically the system will not stop the service because tied to his notification. Plus the service can be stopped from the notification itself.

Maybe with a foreground service started from a device boot broadcast you can have an "always running service"

Related