Reputation: 6419
A service can run in the background indefinitely, even if component that started the service is destroyed.
does the thread has this advantages ?
if we have a thread which downloads file from the internet ,, will it stop if we close the application ? I know that its better to use services , but will threads resume working after the application is closed as services ?
Upvotes: 2
Views: 2885
Reputation: 401
Threads do continue running after an application has closed. However, depending on how much resources the thread is consuming it can be killed at anytime by the OS. Im not sure what your use case is, but because of the high likely hood the thread will be killed, you should almost definitely use a service to do background tasks such as downloading a file.
If you are doing work on a background thread that should not be interrupted (even after the application that started the thread is closed), use a service and start the service in the foreground. This creates an icon in the notification bar which informs the user that some background process is running. Doing so makes your service far less likely to be killed.
From android's documentation:
A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.
Android's documentation on services is a good place to read more:
https://developer.android.com/guide/components/services.html#Foreground
but will threads resume working after the application is closed as services ?
If the thread never gets killed and you reopen your application, then the thread is still running. But again, because of the high chance that the OS will kill your thread, a service should be used for long running background tasks.
Upvotes: 2