What is the difference between AsyncTask and Service in Android?
This is a question that may arise to many developers as they begin to program in Android (This question on Stackoverflow site, and the number of visitors of this question shows that this is in fact the case). The origin of the confusion between these two constructs seems to lie in the fact that there are certain things that you can do with both. Based on that, some developers come to view them as being alternative ways of doing the same thing. A view which is not accurate.
Although, it is true that there are things that you can do with both AsyncTask and Service, they are not just two ways of doing one thing. Each has its own purpose and usage. More importantly, they belong to two different categories of things. Now, lets see what each one is.
In simple terms, software architecture is the organization of highest-level design elements. Or according to Wikipedia:
The term software architecture intuitively denotes the high level structures of a software system. It can be defined as the set of structures needed to reason about the software system, which comprise the software elements, the relations between them, and the properties of both elements and relations.
Among the different architectural patterns, the Layered Architecture Pattern has emerged as the most widely used pattern, particularly for web applications. Most web applications use some kind of variation of this pattern.
The goal of Layered Architecture is to achieve Separation of Concerns (SoC) and modular design in a special and optimal way.
The discussion of Domain Layer is closely related to the topic of Domain Driven Design (DDD) approach, or “philosophy” as its proponents like to call it. Although one does not have to follow DDD in every case, a basic understanding of it helps to clarify, as well as emphasize, the place and role of Domain Layer and Domain Model within a software application.
This basic premise of DDD is that, each software application is built to solve a problem. Therefore, the way the software is
The Service Layers (also called Application Layer) is built on top of the domain layer and other lower layers of the system, and serves as the boundary between the application and the outside world. That is, clients communicate with the application through the service layer.
Image taken from Martin Fowler’s site
The term boundary, in the above description, conveys an important point. If you compare the model in the above picture to the one presented in the Layered Architecture, you will notice that here the Web Layer is not part of the application. That is, Application only encompasses elements - starting from the center and going outward- up to and including the Service Layer, but not beyond that. Here, what we had as the Web Layer in our architecture, can be considered as User Interface Module, located outside the application.