- definition: service, is one of the four components of Android, belongs to the computational type component
- role: provide services that require long-term operation in the background
- Life cycle:
in the The service life cycle, commonly used are:
(1) 4 Methods for manual invocation
Calling methods Manually |
Role |
StartService () |
Start the service |
StopService () |
Close Service |
Bindservice () |
Binding Service |
Unbindservice () |
Unbind Service |
(2) 5 methods of automatic call
Methods for internal automatic calls |
Role |
Oncreat () |
Create a service |
Onstartcommand () |
Start Service |
OnDestroy () |
Destruction Services |
Onbind () |
Binding Service |
Onunbind () |
Unbind Service |
- NOTE: You must declare it in the Androidmanifest.xml file. (All four Android components need to be declared in this configuration file)
- Usage scenarios:
(1) Only start a background service, a long-term task, the use of startservice;
(2) only want to communicate with backstage service, use Bindservice;
(3) If a service is started and bound, the service will always run in the background, using both StartService and Bindservice.
First, OnCreate is always called only once, regardless of how it is called. How many times will the service's Onstartcommand method be called, corresponding to the number of StartService calls. The termination of the service requires both Unbindservice and stopservice to be called simultaneously.
whether StartService and the order in which the bindservice is called, if Unbindservice is called first, the service does not terminate automatically, the service terminates after the StopService is called, and if StopService is called first, the service does not terminate at this time.
and then call Unbindservice or the previous call to Bindservice The service stops automatically after the context does not exist (such as when the activity is finished).
(1) The difference between Service and thread
Note: The service and thread are typically used in conjunction, creating a sub-thread (worker thread) in the service to handle time-consuming operational logic
(2) The difference between service and Intentservice
Full resolution of service in ANDROID development