1. What is a service?
A Service is an application component representing either a application ' s desire to perform a longer-running operation WH Ile not interacting with the user or to supply functionality for another applications to use. (Service is an application component that either meets the long-term background of an application running However, there is no need to interact with the user, or to provide functionality to other applications. )
Two major features of 2.Service:
1): Long-term background operation.
2): no interface.
3. Why use service instead of thread?
We know that a child thread that is turned on with new thread () {}.start () is also not an interface and runs in the background for a long time. So when we want to meet the first feature of the service, why not just use the way to open a new thread?
This question can be answered from the aspect of Android system process management. Once an application is opened in an Android system, normally exiting the application, after emptying the task stack, the application's process does not stop, but is present in the state of an empty process, to facilitate the next quick start (the length of the application startup time is an important performance indicator). However, such a management mechanism will create a problem, that is, the excessive consumption of memory. As the system runs, there is an out-of-memory problem. The Android system has a memory cleanup mechanism. The memory of the system is recycled according to the priority level.
The process is divided into 5 levels of priority: (High-to-low)
1.Foreground processes the process that the foreground process user is manipulating for the application (get focus)
2.Visible process visual processes the user can still see the interface of the process (loses focus).
The 3.Service Process Service application has a service component running in the background.
The 4.Background Process daemon application does not have services running and minimized (activity OnStop).
5.Empty process empty processes application exits without any activity running, the task stack is empty.
When a new process is created or a running process requests a new memory resource, but the system is out of memory, the memory is reclaimed from high to low priority, to meet the new memory requirements. If the thread is a direct new one, when the application exits, its recycle priority is 5, which is the first to be recycled. Therefore, we should not turn on sub-threads in activity for long-running components in the background. Instead, you should create a service that opens a child thread within the service. This will not only ensure Long-term background operation, but also to improve the priority of the process, the system is not easy to reclaim the process, even if the recovery, memory is sufficient, the process will be re-created.
Why use a service without thread-android