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