Comprehensive summary of services in Android

Source: Internet
Author: User
Comprehensive summary of services in Android

1. Service Type

  

By location:

Category Differences Advantages Disadvantages Application
Local) The service is attached to the main process, Services are attached to the main process rather than independent processes, which saves resources to some extent. In addition, the local service does not require IPC or aidl because it is in the same process. Bindservice is much more convenient. After the master process is killed, the service is terminated. Very common applications such as HTC's music play service and the music play service every day.
Remote services) This service is an independent process, The service is an independent process. The corresponding process name format is the package name plus the Android: process string you specified. Because it is an independent process, when the process of the activity is killed, the service is still running and is not affected by other processes. Therefore, it is helpful to provide services for multiple processes with high flexibility. This service is an independent process that occupies a certain amount of resources, and it is a little troublesome to use aidl for IPC. Some services that provide system services are resident.

In fact, remote services are rare and generally system services.

By running type:

Category Differences Application
Front-End Service The ongoing notification is displayed in the notification column, When the service is terminated, the notification in the notification column disappears, which has a certain effect on the user. This is a common music play service.
Background Service The default service is the background service, that is, the ongoing notification is not displayed in the notification column. When the service is terminated, the user cannot see the effect. Some services that do not need to run or stop prompts, such as weather updates, date synchronization, and email synchronization.

Some may ask, can we create an ongoing notification in the background service to become a front-end service? The answer is no. The front-end service calls startforeground (Android 2.0 and later) or setforeground (Android
In versions earlier than 2.0. The advantage of this is that when the service is forcibly terminated by the outside, the ongoing notification will be removed.

 

Categories by usage:

Category Differences
Startservice It is mainly used to start a service to execute background tasks without communication. Stop a service use stopservice
Services started by bindservice Services started by this method need to communicate. Stop services use unbindservice
Startservice is also a service started by bindservice Stepservice and unbindservice should be used to stop the service at the same time.

The life cycle of the services started in the above three methods is also different, which will be given later.

 

2. Differences between services and threads

  

In many cases, you may ask why we need to use a service instead of a thread, because it is convenient to use a thread, which is much more convenient than a service. I will explain it in detail below.

 

1). Thread:Thread is the minimum unit for program execution and the basic unit for CPU allocation. You can use thread to perform some asynchronous operations.

2). Service:A service is a mechanism of Android. If it is a local service during running, the corresponding service runs on the main thread of the main process. For example, oncreate and onstart functions are in the main process when called by the system.
Run on the main thread. For remote service, the corresponding service runs on the main thread of the independent process. Therefore, do not regard the service as a thread. It has nothing to do with the thread's half dime!

In this case, why should we use the service? In fact, this is related to the Android system mechanism. Let's talk about thread first. The thread operation is independent from the activity. That is to say, after an activity is completed, if you do not stop the thread or
If the run method in the thread is not completed, the thread will continue to execute. Therefore, a problem occurs: after the activity is finished, you no longer hold the reference of this thread. On the other hand, you cannot control the same thread in different activities.

For example, if your thread needs to connect to the server for some synchronization after a period of time, the thread needs to run when the activity does not start. At this time, when you start an activity, you cannot control the previously created thread in the activity. Therefore, you need to create and start
Service, create, run, and control the thread in the service to solve this problem (because any activity can control the same service, and the system will only create one corresponding service instance ).

Therefore, you can think of a service as a message service, and you can call context wherever context exists. startservice, context. stopservice, context. bindservice, context. unbindservice, to control it, you can also
Register broadcastreceiver in the service and control it by sending broadcast elsewhere. Of course, these are all threads that cannot be used.

3. Service Lifecycle

 

Oncreate onstart ondestroy onbind 

1). lifecycle of the started service:If a service is started by an activity calling the context. startservice method, the service runs in the background no matter whether any activity is bound to bindservice or unbindservice is unbound from the service. If a service is startservice
The oncreate method is called only once and the onstart method is called multiple times (corresponding to the number of times startservice is called ), in addition, the system will only create one service instance (so you should know that only one stopservice call is required ). The Service will always run in the background, regardless of whether the activity of the corresponding program is running until it is called stopservice or its own stopself method. Of course, if the system resources are insufficient, the android system may also end the service.

 

2). lifecycle of the bound service:If a service is bound to start by an activity calling the context. bindservice method, the oncreate method is called only once no matter how many times bindservice is called, and the onstart method is never called. After the connection is established, the Service will continue to run unless context. unbindservice is called.
If the connection is disconnected or the context of the previously called bindservice does not exist (for example, when the activity is finished), the system will automatically stop the service and the corresponding ondestroy will be called.

 

3). lifecycle of the started and bound service:If a service is started and bound, the Service will continue to run in the background. In addition, no matter how it is called, oncreate will always be called once, and the number of times corresponding to startservice is called, The onstart of service will be called several times. Calling unbindservice will not stop the service, but must call
Stopservice or service stopself to stop the service.

 

4). Clear the service when the service is stopped:When a service is terminated (1. stopservice is called; 2. stopself is called; 3. A bound connection is no longer available (not started), The ondestroy method is called, here you should do some cleanup work, such as stopping the threads created and running in the service.

 

Note:

1. You should know that when calling bindservice to bind to service, you should ensure that unbindservice is called somewhere to unbind (although the binding will be automatically released when the activity is finished,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.