Use and life cycle of Android service

Source: Internet
Author: User

With regard to the use of the service in Android, first understand the service lifecycle, the steps to develop the service are very much like the activity steps, the development service component needs to develop a subclass of service first, Then configure the service in Androidmanifest.xml.

First, let's start with the service life cycle:

As with activity, the service has its own set of life cycle functions, in Android, there are two ways to run a service:

(1) Through the context of the StartService () method: Start the service through this method, even if the visitor exits, the service is still running, because there is no connection between the visitor and the service;

(2) through the Bindservice () method of the context: The service is started by this method, the visitor is bound together with the service, and once the visitor exits, the service terminates.

The lifetime of the service is started from onCreate () and is called until the OnDestroy () method returns.

Like activity, the service initializes its work in onCreate () , releasing the remaining resources in OnDestroy () .

So how do you run a service with the second running mode?

Each created service will potentially allow binding, so an open service may still be bound,

A call to a service that is bound is created when another component (a customer) calls Bindservice () . customers can communicate through an IBinder () interface and service.

customers can use the Unbindservice () method to close the connection, A service can be bound to multiple customers at the same time, and the service is destroyed when multiple customers are unbound.

  

It's better to understand the service's cycle by washing it through the diagram below.

  

The class below defines a service component, and although the service does nothing, it is actually the framework of the service component, and if you want the service to do something, just oncreate () or Onstartcommand () method, you can define the bad business code for the response.

 Public classMyfirstserviceextendsService {PrivateString Tag = "Myfirstservice"; @Override Publicibinder onbind (Intent arg0) {//TODO auto-generated Method Stub        return NULL; } @Override Public voidonCreate () {//TODO auto-generated Method StubLOG.V (Tag, "Service is Created"); Super. OnCreate (); } @Override Public voidOnDestroy () {//TODO auto-generated Method StubLOG.V (Tag, "Service is Destroy"); Super. OnDestroy (); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {        //TODO auto-generated Method StubLOG.V (Tag, "Service is Started"); return Super. Onstartcommand (Intent, flags, Startid); } @Override Public BooleanOnunbind (Intent Intent) {//TODO auto-generated Method StubLOG.V (Tag, "Start onunbind~~~"); return Super. Onunbind (Intent); }       }

After defining the service above, the service needs to be referred to in Androidmanifest.xml:

  <service android:name= "Com.example.service.MyFirstService"/>

Use and life cycle of Android service

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.