Android service details

Source: Internet
Author: User
Service Lifecycle

Refer to self-csdn blog: http://blog.csdn.net/wtao158/archive/2010/01/08/5149721.aspx

 

With the service class, how can we start it? There are two methods:

• Context. startservice ()
• Context. bindservice ()

1. Call the startservice () method anywhere in the same application to start the service. Then the system calls back the oncreate () and onstart () Methods of the service class. In this way, the started service will always run in the background until the context. stopservice () or selfstop () method is called. In addition, if a service has been started and other code tries to call the startservice () method, oncreate () is not executed, but onstart () is re-executed ().

2. Another bindservice () method means to bind the service to the customer class that calls the service. If the customer class called is destroyed, the service is also destroyed. One advantage of using this method is that after the bindservice () method is executed, the service calls back the onbind () method mentioned above. You can return a class that implements the ibind interface from here, the class can communicate with the service by operating on the client, for example, obtaining the service running status or other operations. If the service is not running, the oncreate () method will be used to start the service without calling onstart ().

Summary:

1. startservice () is used to call back the onstart () method. The oncreate () method is called when the service does not exist. If the service exists (for example, bindservice is called before, if the oncreate method of the service has been called), startservice () will skip the oncreate () method.

2. bindservice () is used to call back the onbind () method. It serves to build a bridge between the service and the caller, not responsible for more work (for example, a service needs to connect to the server), usually bindservice is used to bind to an existing service (that is, the service started through startservice ).

Because the onstart () method of the service is called only when startservice () starts the service, pay attention to this when using onstart.

 

Communicates with the service and keeps it running

What if we want to maintain communication with the service and do not want the service to exit with the activity? You can first startservice () and then bindservice (). When you do not need to bind, run the unbindservice () method. executing this method will only trigger the onunbind () of the service and will not destroy the service. In this way, the communication with the service can be maintained and will not be destroyed with the activity destruction.

 

Improve Service priority

The Android system has its own method for memory management. In order to ensure the orderly and stable operation of the system, the system automatically allocates memory to control the memory usage of the program. When the system finds that the current resources are very limited, it will kill programs or services that he thinks are not important to ensure that some programs with higher priority can run. This ensures that the programs that are truly useful to users are still running. If your service encounters this situation, it will probably be killed first. However, if you increase the service priority, you can leave it for a while. We can use setforeground (true) to set the service priority.

Why is it foreground? By default, the started service is marked as background, and the currently running activity is usually marked as foreground, that is to say, if you set foreground for the service, the priority of the running activity will be improved. This does not guarantee that your service will never be killed, but increases the priority of your service.

 

From other network materials: about service Lifecycle

The lifecycle of the android service is similar to that of the activity, but there are also important differences in some details:

Oncreate and onstart are different.

You can start a service by calling the context. startservice (intent) method from the client. If the service is not running, Android starts it and calls its onstart method after the oncreate method. If the service is already running, its onstart method will be called again by the new intent. Therefore, the onstart method of a single running service is completely possible and normal.

Onresume, onpause, and onstop are not required.

A callback service usually has no user interface, so we do not need the onpause, onresume, or onstop method. A running service always runs in the background at any time.

Onbind

If a client needs to connect to a service permanently, it can call the context. bindservice method. If this service does not run the method, the oncreate method will be called to create the service but the onstart method will not be called to start it. On the contrary, the onbind method will be called by the intent of the client, and it returns an ibind object so that the client can call this service later. It is normal that the same service is started and bound by the client at the same time.

Ondestroy

Like activity, the ondestroy method is called when a service is terminated. Android terminates a service when no client is started or bound to a service. As with many activities, Android may end a service when the memory is low. If this happens, Android may try to start the terminated service when the memory is sufficient. Therefore, your service must save the persistent information for restart, and it is best to do so in the onstart method.

Related Article

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.