Android supports standard Java Threads. You can use standard Threads and the tools from the package “java.util.concurrent
” to put actions into the background. The only limitation is that you cannot directly update the UI from the a background process.
If you need to update the UI from a background task you need to use some Android specific classes. You can use the class “android.os.Handler
” for this or the class “AsyncTask
”
The class “Handler
” can update the UI. A handle provides methods for receiving messages and for runnables. To use a handler you have to subclass it and override handleMessage()
to process messages. To process Runable
, you can use the method post();
You only need one instance of a handler in your activity.
You thread can post messages via the method sendMessage(Message msg)
or sendEmptyMessage
.
If you have an Activity
which needs to download content or perform operations that can be done in the background AsyncTask
allows you to maintain a responsive user interface and publish progress for those operations to the user.
For more information you can have a look at these links.
http://mobisys.in/blog/2012/01/android-threads-handlers-and-asynctask-tutorial/
http://www.slideshare.net/HoangNgoBuu/android-thread-handler-and-asynctask