Can a Service's child process be killed?
If I have a foreground service running which spawns a thread, can that child thread ever be killed by the Android OS? Or is that child thread protected as well just like the Service? To prevent the child thread from being killed, do I need to make it into its own Service?
Thanks, Nick
Your service is a component. You indicate that it is a "foreground service", indicating that you called startForeground()
. So long as you do not stop it, the foreground service will tend to keep the process -- and its threads by extension -- free from termination, though this is not a guarantee.
Android never terminates threads that you fork, except by terminating the entire process.
An AsyncTask started by a service, as well as a normal thread, is not killed if the service that started it is killed. In other words, you should clean up your threads when your service gets killed.
See the lifecycle of Service: http://developer.android.com/reference/android/app/Service.html#ServiceLifecycle
You might want to consider using an IntentService:
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand.
精彩评论