Search code examples
javaandroidandroid-servicebackground-service

Android Service required to run in background until the app is killed


I have been reading Android docs and I feel I am bit lost and confused.

What is the type of service I need to use in Android, so that I can keep running my code even when the app is paused or minimized for prolonged period.

I am not interested in running the service if the app is closed. I want to run small piece of code that will run when app is in foreground or background, but not killed.


Solution

  • You can use a bound Service. The Service will stop after all the bound clients disconnect. Your Activity binds to the Service and when your Activity is killed or finished, you unbind and the Service stops. If Android kills off the Activity, the bound connection is also shut down and your Service will stop.