Initial contact of the Android service

Source: Internet
Author: User

Just read a little service-related books, do notes, and hope to help people in need.

First of all, the development of service and activity is similar, also requires two steps:

First, define a subclass that inherits the service

Second, configure the service in the Androidmanifest.xml file

The service life cycle method is as follows:

1. Abstract IBinder onbind (Intent Intent): This method is a method that the service subclass must implement. The method returns an IBinder object that the application can use to communicate with the service component

2, void OnCreate (): The method is immediately called back when the service is created for the first time

3. void OnDestroy (): Callback the method before the service is closed

4, int onstartcommand (Intent Intent, int flags, int startid): The client calls the StartService (Intent) method to call back the method when the service is started

5. Boolean onunbind (Intent Intent): The method will be recalled when the client used to bind the service disconnects

Two ways to start service in Android:

1, through the context of the StartService () method: The service initiated by this method, the visitor is not associated with the service, even if the visitor exits, the service continues to run.

2, through the context of the Bindservice () method: The service started with the method, the visitor is bound to the service, and when the visitor exits, the service exits and stops running.

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.