A detailed description of the Android service component

Source: Internet
Author: User

This article focuses on some of the basics of getting Started with Android service and wants to take you to the door of Android Service.

1. What is a service

The service is a solution in Android that runs in the background of the program, and is ideal for programs that do not require interaction with the user and require long-term Operation. The operation of the service does not depend on any user interface, even if the program is switched to the background, or if the user opens another application, the service remains operational.

2. How to define a service

Usually we'll create a new class that inherits the service

Such as:

public class MyService extends service{

Public IBinder Onbind (Intent Intent) {

}

public void OnCreate () {

}

public int Onstartcommand (Intent Intnt,int flags,int startid) {

return Super.onstartcommand (intente,flags,startid)

}

public void Ondestory () {

Super.ondestory ();

}

}

OnCreate () starts when the service is created, Onstratcommand () starts when the service is started, and Ondestory () is called when the service is Destroyed.

3. How to start the service

The first thing to do is to register the service in androidmanifest, such as:

<service name= ". MyService ">

</service>

The start-up service needs to be via method StartService (Intent), such as StartService (new Intent (this,myservice.class));

Stopping the service requires a method StopService (intnet), such as StopService (new Intent (this,myservice.class));

OnCreate () is called before the service is started, and then Onstartcommand () is Called. Where OnCreate () is invoked the first time the service is created, and Onstartcommand () is invoked every time the services are Started.

4. How the service communicates with the activity

Although we started the service through startservice, it was just a running service and there was no contact with the activity, so now we have to communicate the services and Activities.

Here we need to use the Onbind () mentioned above, but first we will create an inner class that inherits the binder in the class that inherits the service, and the inner class writes the methods that need to be used, such as the getprogress () to see the download progress. and start downloading the startdownload () and so on, in order for the activity to get to the instance of the class, we also need to create an instance to return the instance in Onbind (). An example of the whole:

public class LocalService extends Service {

public class Downloadbinder extends binder{

public void Startdownload () {

LOG.D ("myservice", "startdownload execute");

}

public int getprogress () {

Return 0;

}

}

Public Downloadbinder mbinder=new Downloadbinder ();

@Override

Public IBinder Onbind (Intent Intent) {

Returns the Simplebinder object

Return mbinder;

}

}

So how does the next activity get binder?

As long as we invoke Onbind (intnet,serviceconnection,bind_auto_create) in the activity, we can bind the service and the activity so that the activity invokes the methods in the Service.

The second parameter, serviceconnection, needs to be created in the activity, we need to create its anonymous class in the activity, and then there are two methods, one for onserviceconnected (), The other is onservicedisconnected (),

We need to get the binder in the service through a down transformation in onserviceconnected (), and then call the methods in the service through Binder.

The third parameter is a flag bit, and Bind_auto_create automatically creates the service after binding on behalf of the activity and Service.

If you need to unbind the service, call Unbindservice (serviceconnection);

Example

Serviceconnection sc = new Serviceconnection () {

@Override

public void onservicedisconnected (componentname Name) {

}

@Override

public void onserviceconnected (componentname name, ibinder Service) {

Myservice.downloadbinder Mbinder = (myservice.downloadbinder) service;

}

};

Findviewbyid (r.id.btnbind). setonclicklistener (new onclicklistener () {

@Override

public void OnClick (View V) {

Bindservice (new Intent (main.this,myservice.class), sc, context.bind_auto_create);

Isbind = true;

}

});

Findviewbyid (r.id.btnunbind). setonclicklistener (new onclicklistener () {

@Override

public void OnClick (View V) {

If (isbind) {

Unbindservice (sc);

Isbind = false;

}

}

});

5. Life cycle of the service

Start-only service: onCreate () →onstartcommand () →ondestory ()

If StartService () is started multiple times, onCreate () executes only once, OnStart () executes multiple times, creating only one instance of the Service.

Only services bound: onCreate () →onbind→onunbind () →ondestory ()

Bindservice () multiple calls, onCreate () is called only once, and the service runs until Unbindservice () is Called.

A service that starts and binds: onCreate () →onstartcommand () →onbind () →onunbind () →ondestory ()

OnCreate () must be executed only once, StartService calls several times Onstartcommand (), call unbind do not stop service, stopservice or service stopself () Stop the Service.

Article from: Blog Park/shawdo

A detailed description of the Android service component

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.