AsyncTask:
AsyncTask enables proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
AsyncTask
is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks
should ideally be used for short operations (a few seconds at the most.)
If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent
package such as Executor
, ThreadPoolExecutor
and FutureTask
.
Thread:
Moving numerous or long tasks from the main thread, so that they don’t interfere with smooth rendering and fast responsiveness to user input, is the biggest reason for you to adopt threading in your app.
Use it to separate long running computation from Main Thread ( UI Thread)
Service:
A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface.
A service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
IntentService:
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand.
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
Loader:
Loader API lets you load data from a content provider or other data source for display in an Activity or Fragment.
Loaders solve these problems and includes other benefits. For example:
Loaders run on separate threads to prevent janky or unresponsive UI.
Loaders simplify thread management by providing callback methods when events occur.
- Loaders persist and cache results across configuration changes to prevent duplicate queries.
- Loaders can implement an observer to monitor for changes in the underlying data source
So, is Asynctask really so bad and i should use framework for networking tasks? And what should i use for background (not networking) task?
Use AsyncTask
to handle work items shorter than 5ms in duration. You can use either Thread
Or Service
or IntentService
for the background task.