I have a block of code
list = geocoder.getFromLocation(
locationNetwork.getLatitude(),
locationNetwork.getLongitude(), 3);
where i am trying to retrieve the last location via getFromLocation(LocationManager). Since that can take time i want to put it on a different thread than the UI. But i am confused what should be used. Should a handler or Async task be used for this purpose. I am confused as when an handler and Async task should be used in Android. Can any one explain me with some example,scenarios..
Thanks.!

- 9,022
- 10
- 51
- 59
-
it just required a simple thread, you need to call the `geocoder.getFromLocation()` method in a separated method, and in another method handle the UI [this tutorial](http://arashmd.blogspot.com/2013/06/java-threading.html#synctr) would help you – Jul 03 '13 at 19:40
2 Answers
I think you're getting mixed up here. Handlers and an Async task are two different things. A handler is used to communicate between threads, see here, while an Async task is basically an easier to use thread in Android. If you create a new thread and want to communicate with another thread, you have to use a Handler. However, Google made it easier by providing the Async task class which allows communication with the main UI thread without the use of handlers, see here. So in short, use an Async task for your purpose. The link I have provided actually provides an example usage and goes into depth about Asyncs. If you need clarification let me know.

- 7,811
- 3
- 25
- 30
-
-
1Asynctasks have a method called onPostExecute that you must implement. This method runs on the main UI thread after the Async task completes. This way, any kind of communication you want done with the main thread can be done in this method. It's much easier than using Handlers to send a message back to the main UI thread. – btse Jul 03 '13 at 19:45
See this SO answer it talks about the difference of AsyncTask
, Handler
, and Thread
Most of the time it is developer preference, if you are talking about creating a Thread
. If you need to update the UI
, especially while the background Thread
is running, then I think AsyncTask
is easier. Both of them allow you to continue working with the UI
while they do the heavier lifting in the background.
You can use an AsyncTask
and do the work in doInBackground()
then update the UI
on any of the other AsyncTask
methods. If you might want to use this Thread
in more than one place then make it a separate file and you can pass Activity Context
to it through its constructor if you need it. If you will only use it in one Activity
then you can make it an inner class of your Activity
then it will have access to all member variables of that Activity
-
i get the working of Async Task. I am just confused as when we can use handler over Async Task... – user2511882 Jul 03 '13 at 19:45
-
-
@user2511882 Use AsyncTask when the caller thread is the UI thread. Use handlers elsewhere. – btse Jul 03 '13 at 19:50
-
If you create your own worker thread, then you have to use Handler to communicate back to your main(UI) thread. For AsyncTasks, the onPreExecute()/onPostExecute() methods do this work for you. – Android Noob Jul 03 '13 at 19:51
-
-
[Here is an example](http://stackoverflow.com/questions/11771506/update-activity-from-service-using-handler) I hope that helps clear it up a little – codeMagic Jul 03 '13 at 19:58