I want to perform some rather short tasks in a background thread. However, this tasks are very sensitive and must be as reliable as possible.
My first idea was to use a (again short living) foreground Service. I start the service, do the tasks in a separate thread and stop it when they are finished. Sound simple, similar to what robospice is doing, however the documentation of Service does not say anything about killing threads. As Service itself is just an object and not a thread, how am I suppose to “tell” the system that a certain thread belongs to the service and should not be killed?
- Is it sufficient to just hold a reference to the Thread object in my Service object?
- Should I use a HandlerThread and hold a reference to the looper? (Like IntentService)
- Should I also extend the Thread class and bind to the service? 🙂
Hope someone can give me some directions.
Thanks.
Threads got killed only with whole process. Android can’t kill any patricular thread. Android can kill whole process and all it’s threads. So you should no let android kill your application and all your thread will be alive.
Android won’t kill your app if it has foreground activity or service or there is enought resource to keep your app alive.