开发者

AsyncTask and Background Thread when to Use

What is the difference between AsyncTask and Background Thread. Which should be preferred or any scenarios to use these?

What I am trying to achieve right now is Will be sending request to server when开发者_StackOverflow user goes on a specific activity and display the data received on the same activity? The data received may be images or some text which I need to display in TextView or ListView.


An AsyncTask is basically a wrapper class for a Java thread. It provides a convenient mechanism for executing one-time blocking operations. Background threads are more useful when you have a task that's long-lasting and/or permanent to the entire course of the Activity (although I suppose you could implement AsyncTask to be permanent and just update the UI through the progress mechanism).

In your case, I would implement an AsyncTask. Do your request in doInBackground() then update the UI in onPostExecute().


There is no difference. A AsyncTask is a background thread. It's an implementation which helps you to perform tasks in background. Read its documentation and you will see ;-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜