The service cannot run on its own. You need to call the context. startservice () or context. bindservice () method to start the service.
Both methods can start the service, but they are used in different places:
(1) The startservice () method is used to start the service. There is no association between the caller and the service. Even if the caller exits, the service can still run.
(2) Use the bindservice () method to start the service. The caller and the service are bound together. Once the caller exits, the service is terminated.
(3) If you use the startservice () method to start the service, the system will first call the oncreate () method of the service before it is created, and then call the onstart () method. If the service has been created before the onstart () method is called, multiple call of the startservice () method will not lead to multiple creation of the service, but will lead to multiple calls of the onstart () method. A service started using the startservice () method can only end the service by calling the context. stopservice () method. The ondestroy () method is called when the service ends.
(4) If you use the bindstart () method to start the service, the system will first call the oncreate () method of the service before it is created, and then call the onbind () method, the caller and the service are bound together. If the caller exits, the system first calls the onunbind () method of the service and then calls the ondestroy () method. If the service has been bound before the bindservice () method is called, multiple calls to the bindservice () method will not result in multiple service creation and binding (that is, oncreate () and onbind () method is not called multiple times ). If the caller wants to unbind from the service being bound, call the unbindservice () method. Calling this method also causes the system to call the onunbind () and ondestroy () Methods of the service.