Android Development Service

Source: Internet
Author: User

Creation of service:

Create a new class that inherits from the service

Service is also one of Android's four components and needs to be configured in the manifest file

Service life cycle:

I. Opening via StartService

OnCreate, Onstartcommand (), OnDestroy ()

After the other component calls StartService (), the OnCreate () is executed first, then Onstartcommand () is executed until the stopself () service is called by itself or another component to stop and eventually be destroyed by the system.

Two. Open via Bindservice

OnCreate (), Onbind (), Onunbind (), OnDestroy ()

Service is turned on by Bindservice, call Unbindservice to unbind services



Bind mode start service:

The Bingservice () method needs to receive three parameters

The first parameter is a intent object that specifies the service to start

The second parameter is a Serviceconnection object that listens for the connection state between the caller and the service

The third parameter is flags, which specifies whether the service is automatically created, specified as 0 is not automatically created, and specified as Bind_auto_create is automatically created



In addition to overriding the Oncreate,onbind,onunbind method, the service requires an inner class and inherits from IBinder, which defines the method that can communicate with the caller

The caller needs to create an inner class that inherits from Serviceconnection to implement the connection service

Converts an IBinder object in an overridden Onserviceconnection method to its own class object

Android Development Service

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.