Android service-terminate the service

Source: Internet
Author: User

A started service must manage its own lifecycle. That is to say, unless the system needs to recycle the system memory, the system will not terminate or destroy this service. After the onstartcommand () method returns, this service will continue to run. Therefore, this type of service must be terminated by calling the stopself () method or another component by calling the stopservice () method.

Once you use the stopself () method or the stopservice () method to request termination of the service, the system may destroy the service as soon as possible.

However, if your service processes multiple requests to the onstartcommand () method at the same time, you should not terminate the service when you complete the request startup process, because your service may be receiving a new start request (terminating the service at the end of the first request may terminate the second request ). To avoid this problem, you can use the stopself (INT) method to ensure that the service you request to terminate is always based on the recently started request. That is to say, when you call the stopself (INT) method, you must pass the service ID to be terminated to this method (this ID is sent to the onstartcommand () method ). In this way, if the service receives a new start request before you call the stopself (INT) method, the ID will not be terminated because of the mismatch.

Warning:It is important that your applications terminate their services at the end of their work to avoid wasting system resources and battery power. If necessary, other components can call the stopservice () method to terminate the service. Even for a service that can be bound, if the service receives a call to the onstartcommand () method, you must terminate the service by yourself.

For more information about the service lifecycle, see "manage Service lifecycle"

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.