开发者

AsyncTask and Background Thread when to Use

开发者 https://www.devze.com 2023-04-10 00:24 出处:网络
What is the difference between AsyncTask and Background Thread. Which should be preferred or any scenarios to use these?

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

精彩评论

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

关注公众号