1, the role of Android service:
Service is typically used to handle time-consuming operations , or background execution does not provide a user interface , for example: downloading, playing music.
2. life cycle of Android service:
The service life cycle is not as complex as the activity lifecycle, it has only OnCreate (), OnStart (), OnDestroy ( ) Three methods, and when the service is first started , The OnCreate (), OnStart () methods are called successively , when the service is stopped , the OnDestroy () method is executed, and it is important to note that if the service is started , when we start the program again to invoke the service, the OnCreate () method is not executed, but the OnStart () method is executed directly .
3. Two ways to start Android service:
①,StartService (): The same app calls the StartService () method anywhere to start the service, and then the system automatically calls OnCreate (), OnStart (), so that the service started will run in the background until the StopService () or Selfstop () method is called. In addition, if a Service has been started and other code wants to call the StartService () method again, the OnCreate () will not be executed, but the OnStart () will be executed again.
②,Bindservice (): When started with the Bindservice () method, the service is bound to the client and the client stops and the service stops.
The difference between the two : StartService () is not associated with whether the client exits, even if the client stops running the service also runs in the background , but Bindservice () is bound to the client, The client exits the service and stops automatically .
Android Service Introduction and startup mode