When to use a Service or AsyncTask or Handler?

前端 未结 5 821
夕颜
夕颜 2020-12-22 23:20

Can someone tell me the TRUE difference?

5条回答
  •  时光取名叫无心
    2020-12-22 23:48

    Use Service when you've got something that has to be running in the background for extended periods of time. It's not bound to any activity. The canonical example is a music player.
    AsyncTask is great when some stuff has to be done in background while in the current activity. E.g. downloading, searching for text inside a file, etc.
    Personally I use Handlers only to post changes to the UI thread. E.g. you do some computations in a background thread and post the result via handler.

    The bottom line: in most cases, AsyncTask is what you need.

提交回复
热议问题