AsyncTask or Handler- Which one is better for time

2020-07-18 06:01发布

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.

8条回答
叛逆
2楼-- · 2020-07-18 06:31

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楼-- · 2020-07-18 06:32

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.

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-07-18 06:35

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.

查看更多
做个烂人
5楼-- · 2020-07-18 06:35

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楼-- · 2020-07-18 06:38

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

查看更多
地球回转人心会变
7楼-- · 2020-07-18 06:44

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.

查看更多
登录 后发表回答