Sending messages between threads using activity thread queue and Handler class
Problem:You need to pass information or data from a Service or other background task, to an Activity. Since Activities run on the UI thread it is not safe to call them from a background thread.
Solution: Write a nested class that extends Android's Handler class; override the handleMessage() method which will read messages from thread queue;
Pass this Handler to the worker thread, usually via the worker class' constructor;
In the worker thread, post messages using the various obtainMessage() and sendMessage() methods;
This will cause the Activity to be called at the handleMessage() method, but on the event thread so you can safely update the GUI.
Discussion:There are many situations when it is required to have a thread running in the background and send information to main Activity's UI thread. At the architectural level there are different approaches that can be taken:
Use of Android AsyncTask class
Start a new thread
Though using AsyncTask is very convenient, there are a situations when you really need to construct a worker thread by yourself. In such situation you likely will need to send some information back to Activity thread. Keep in mind that Android doesn't allow other threads to modify any content of main UI thread. Instead you're required to wrap data into Messages and send them through message queue.
The implementation consists of two parts.
1) Add handler ==
Add an instance of Handler class to e.g., your MapActivity instance.
In the worker thread post a message to activity main queue whenever you need Add handler class instance to your MapActivity instance.
Downloads:(not handled yet)