AsyncTask or Handler- Which one is better for time

2020-07-18 06:35发布

问题:

I am new to android development. Currently i am working on an application which will take a query from user, send the query to the server and will receive an XML response in return. Then it will process the xML response using XMLparser, will extract the answer from XML response and display answer to the user. Since all this processing takes some time, i want to show a progress dialog to the user for the processing time it takes.

I have gone through some ways of doing this like i can use AsyncTask, Handler or Runnable (java) threads. But as much I have read about all these, I have got confused which one to use. Please tell me which one is better to use for above mentioned scenario. And a simple way to implement the preferred one.

Thank You in advance.

回答1:

I'd recommend you to use AsyncTask because it is simplier than other approaches and it suits your needs.

I think you mess a bit these three different entities:

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

Runnable is a command that can be performed. You should run in a separate thread. Also you should develop a logic in this case how to update progress and how to notify when the task is finished.

Handler is a special class that can handle messages that are sent to the handler's thread.



回答2:

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

This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

AsyncTask is designed for a use very similar to yours.

Runnable is only to run a block of code on another thread:

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

Handler is used more for Message Queuing. Your case doesn't seem to require messages being sent repeatedly.

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own. http://developer.android.com/reference/android/os/Handler.html

Note that neither Runnable nor Handler talk about displaying the results back on the UI thread. That is where AsyncTask helps you more than either of these.



回答3:

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



回答4:

As far I know AsyncTask is the recommended way. I think is the easiest way to implement and the more "Android best practice" for asynchronous tasks.

Could you refer to this question



回答5:

Here's how I see it.

Handler is more for queuing many actions, and gives a bit more control. It's better for repetitive tasks which are generally not restricted to the UI.

AsyncTask provides a simple way to do background processing, not caring much about the lower-level stuff. It's great for relatively small, individual UI updates.

IMO, you should use AsyncTask. That being said, it's kind of a toss-up.



回答6:

I think it's a matter of self-preference, but in your case I would go for the AsyncTask because it facilitates the interaction between the UI thread and the background thread.



回答7:

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

To overcome this, I ran the AsyncTask and catched the result with a custom callback method, which simply encapsulate it inside a Message and sends it to my custom Handler, which is inside the UI thread and can safely render on-screen my result.



回答8:

AsyncTask might be the choice,because it provides all necessary controls for starting up the async task, updating progress bar, etc.

But, the point is AsyncTask is the best solution to the scenario. Handler or Runnable are more suitable to duplex cases, like chat apps.