When to use a Service or AsyncTask or Handler?

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

Can someone tell me the TRUE difference?

5条回答
  •  温柔的废话
    2020-12-22 23:45

    To complement the other answers here regarding the distinction between service and AsyncTask, it is also worth noting[0]:

    • A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
    • A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).

    Services tend to be things that describes a significant part of your application - rather than an AsyncTask which is typically contributes to an Activity and/or improves UI responsiveness. As well as improving code clarity Services can also be shared with other applications, providing clear interfaces between your app and the outside world.

    Rather than a book I would say the developer guide has lots of good answers.

    [0] Source: http://developer.android.com/reference/android/app/Service.html#WhatIsAService

提交回复
热议问题