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