Android----Service

Source: Internet
Author: User
Service

Use the service when you need to create a program that runs in the background.

Service can be divided into two kinds: infinite life and limited life.

It is particularly important to note that the service is different from the activities (which is simply understood as the difference between the background and the foreground), for example, if you need to use a service, you need to call StartService (), thus StartService () Go to invoke the OnCreate () and OnStart () methods in the service to start a background service.

First, start the service:

Context.startservice ()->oncreate ()->onstart ()->service running

Note: where OnCreate () can perform some initialization of the service, OnStart () starts the service.

Second, stop service:


Context.stopservice () |->ondestroy ()->service stop

  Description: The next instance is a small example of playing music using background services, click Start to run the service, and click Stop Service.

Source code for Mainactivity:

Mainactivity

Source code for MyService:

Mainservice

Android----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.