AsyncTask or Handler - which one is better for time-consuming networking and processing?

I am new to Android development. I'm currently working on an application that will take a request from a user, send a request to the server and receive an XML response in response. It will then process the xML response using XMLparser, extract the response from the XML response, and display the response to the user. Since all this processing takes some time, I want to show the user a progress dialog for the processing time.

I have done several ways to do this, how can I use AsyncTask, Handler or Runnable (java) threads. But as far as I read about all these things, I got confused which one to use. Please tell me which one is better to use for the above scenario. And an easy way to implement your preferred option.

Thank you in advance.

+4
source share
8 answers

I would recommend you use AsyncTask, because it is simpler than other approaches and fits your needs.

I think you messed up these three different objects a bit:

AsyncTask launches a set of actions in a separate thread, can show progress during its work and show a notification when the task is completed.

Runnable is a command that can be executed. You must run in a separate thread. You should also develop logic in this case, how to update progress and how to notify when a task is completed.

A handler is a special class that can process messages sent to a handler stream.

+2
source

From http://developer.android.com/reference/android/os/AsyncTask.html :

This class allows you to perform background operations and publish results to a user interface thread without the need to manipulate threads and / or handlers.

AsyncTask is for use very similar to yours.

Runnable only to run a block of code in another thread:

Represents a command that can be executed. Often used to run code in another thread. (Http://developer.android.com/reference/java/lang/Runnable.html)

The handler is used more for the message queue. It seems that your case does not require resending messages.

For the handler, two main methods are used: (1) plan messages and executable files that should be executed as some points in the future; and (2) set up an action to be performed on a thread other than your own. http://developer.android.com/reference/android/os/Handler.html

Note that neither Runnable nor Handler talk about displaying results in the user interface thread. This is where AsyncTask helps you more than any of them.

+2
source

This may be my personal preference - but I would use AsyncTask in your case. It provides all the necessary tools to launch a task, update the progress as necessary, etc. I have a very similar requirement in my application (send a request to the server, get a response in XML, parse the response, do something with the data) m using AsyncTasks for this purpose.

+1
source

As far as I know, AsyncTask is recommended. I think this is the easiest way to implement and more “Android best practice” for asynchronous tasks.

Could you refer to this question

0
source

This is how I see it.

Handler more suitable for queues of many actions and gives a little more control. This is best for repetitive tasks that are usually not limited to the user interface.

AsyncTask provides an easy way to process background images without worrying about lower-level materials. This is great for relatively small, individual user interface updates.

IMO, you should use AsyncTask . This, as they say, as if throws up.

0
source

I think this is a self-assessment question, but in your case I would go for AsyncTask because it facilitates the interaction between the UI thread and the background thread.

0
source

I would use a combination of AsyncTask and Handler , because please remember that you cannot change the user interface outside the user interface thread (in this case you cannot intervene and show the answer to the user).

To overcome this, I launched AsyncTask and found the result using a special callback method that simply encapsulates it inside the Message and sends it to my custom Handler , which is inside the UI thread, and can safely display my result on the screen.

0
source

AsyncTask may be the choice as it provides all the necessary controls to run the async task, update the progress bar, etc.

But AsyncTask is the best solution for the script. A handler or runnable is more suitable for duplex cases such as chat applications.

0
source

Source: https://habr.com/ru/post/1392044/


All Articles