Use of service (services) in Android

Source: Internet
Author: User

Priority of the process
---------------------------------
The priority of a process is that the higher the priority, the stronger the "vitality" of the process, and the lower the lower-priority process, which is more likely to be purged by the Android system.
The priority of the process is high-to-Low:
1. Foreground process
2. Visible processes
3. Service Process
4. Background process
5. Empty process


Service (services)
---------------------------------
Service is a core component of the Android system, created, maintained, and managed by Android.
The service needs to be registered in the Androidmanifest.xml file.
All the service developers develop need to inherit from the Android.app.Service class.
The service has no interface and is suitable for performing time-consuming tasks in the background (that is, invisible).
Although the service is suitable for performing time-consuming tasks, the service is still running on the main thread, so when performing a task, the child thread should be turned on to complete.
The service is singleton.


Steps to develop a service
---------------------------------
1. Custom class inheritance Android.app.Service, because the Service is abstract, after implementing the inheritance relationship, the custom class needs to override the abstract method Onbind (), currently, ignoring the method, only guarantee that the method exists can
2. Open the Androidmanifest.xml file, under the <application> node, add the <service> node (same as the <activity> node, and do not differentiate the order), and configure at least The Android:name property of the <service> node, the value of which is the full name of the custom service class, the package name + class name, for example Cn.tedu.app.SampleService


Start Service
---------------------------------
In any contextwrapper subclass (such as activity), call the StartService (Intent Intent) method to activate the service component


Close Service
---------------------------------
Method 1: In any contextwrapper subclass, call the StopService (Intent Intent) method to close/Stop the service
Method 2: Inside the service, call the Stopself () method to close itself


Life cycle of Service
---------------------------------
OnCreate (): Called when a service component is created (1th activation), repeatedly activating the same service component does not repeatedly call the method
Onstartcommand (): Called when the service component is activated, the method is called each time it is activated, and the method is executed after OnCreate () at the 1th activation time.
OnDestroy (): Called when the service component is destroyed


Stickiness of Service
---------------------------------
The stickiness of the service is that the service is automatically restarted when the service is accidentally destroyed
The stickiness of the service is determined by the return value of the Onstartcommand () method, and the value of the return value can be:
A) Start_sticky: sticky
b) Start_not_sticky: Non-sticky
c) start_sticky_compatibility: Sticky in compatibility mode
d) start_redeliver_intent: sticky, and will carry the original active service INTENT object when the service is automatically restarted

Use of service (services) in Android

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.